プロが教えるわが家の防犯対策術!

ファイルを保存する時、キャンセルをするとタイトルどおりのエラーメッセージが出ます。当方、初心者でデバッグの仕方がわからず困ってます。どなたか教えてください。

コードの一部
Dim myFile As String

Private Sub Command2_Click()
CommonDialog1.Filter = "テキスト(*.txt)|*.txt|すべて(*.*)|*.*"
CommonDialog1.FilterIndex = 1
CommonDialog1.Flags = cdlOFNOverwritePrompt '上書き確認する
CommonDialog1.ShowSave '!!!デバッグの際はこの行が反転表示されます!!!
If CommonDialog1.FileName = "" Then Exit Sub
myFile = CommonDialog1.FileName
FileWrite
Form1.Caption = "Form1" & myFile
End Sub

Private Sub FileWrite()
Dim buf As String
Open myFile For Output As #1
Print #1, RichTextBox1.Text; '最後の';'は余計な複改を入れないため
Close #1
Exit Sub
End Sub

A 回答 (4件)

エラー処理を入れました。

そのほかに便宜上
command2--->1,
RichTextBox1.Text; -->"aaa"に変えましたが
私の場合上手くいきました。ご参考に。
Dim myFile As String

Private Sub Command1_Click()
CommonDialog1.Filter = "テキスト(*.txt)|*.txt|すべて(*.*)|*.*"
CommonDialog1.FilterIndex = 1
CommonDialog1.Flags = cdlOFNOverwritePrompt '上書き確認する
On Error GoTo error1
CommonDialog1.ShowSave '!!!デバッグの際はこの行が反転表示されます!!!
If CommonDialog1.FileName = "" Then Exit Sub
myFile = CommonDialog1.FileName
MsgBox myFile
FileWrite
Form1.Caption = "Form1" & myFile
Exit Sub
error1:
MsgBox Err.Description
Err.Clear
End Sub

Private Sub FileWrite()
Dim buf As String
Open myFile For Output As #1
Print #1, "aaa" 'RichTextBox1.Text; '最後の';'は余計な複改を入れないため
Close #1
Exit Sub
End Sub

この回答への補足

エラーが出ずに処理できたのは良いのですが、出来れば、メッセージボックスを出さずに、そのまま今のプログラムを続けたいのですが、どうすればよいか教えていただけないでしょうか。

補足日時:2002/08/27 18:28
    • good
    • 0
この回答へのお礼

ありがとうございます。助かりました。

お礼日時:2002/08/27 18:27

こんにちは。

maruru01です。

No.2の人が挙げているので、付けたし程度ですが。
コモンダイアログでは一般的には、CanselErrorプロパティにTrueを入れる方法を使用します。

Private Sub Command2_Click()

  With CommonDialog1
    .CancelError = True
    .Filter = "テキスト(*.txt)|*.txt|すべて(*.*)|*.*"
    .FilterIndex = 1
    .Flags = cdlOFNOverwritePrompt
    On Error Goto ErrHandler
    .ShowSave
    myFile = .FileName
  End With

  FileWrite
  Form1.Caption = "Form1" & myFile

  Exit Sub

ErrHandler:
  '[キャンセル]ボタンクリック時(何もしない)

End Sub

なおキャンセルボタンクリック時に、myFileをクリアする処理を書いてもいいでしょう。(Form1のキャプションも変更しなくてはいけないでしょうが。)
    • good
    • 0

エラー番号は、err.numberで取得できます。

(ERRオブジェクトのヘルプを参照)
今回はエラー番号がわかるのだから、1のサンプルの最後の方を

error1:
if err.number=32755 then
 '何もしない
else
 MsgBox Err.Description
 '(ここは本当のエラーなので処理を中断するような記述が必要かも)
end if

Err.Clear
End Sub

とすればいいのでは?
    • good
    • 0

On Error Resume とか


On Error Resume Nextというのもあります。
CommonDialog1.CancelError=trueダイアログキャンセルをエラーとする、などもあります。
http://www-user.interq.or.jp/~komurak/err/
    • good
    • 0

お探しのQ&Aが見つからない時は、教えて!gooで質問しましょう!