我用CommonDialog控件对话保存一个文件。
         CommonDialog1.ShowSave
          CommonDialog1.Filter = "(*.dat)|*.dat"
          strFileName = CommonDialog1.FileName
          If Dir(strFileName) <> "" Then
                  If MsgBox("文件已经存在,是否覆盖?", vbInformation + vbOKCancel, "提示") = vbCancel Then
                          Exit Sub
                  End If
          End If
 为什么对话框出来时我点击“取消”按钮,没有退出该对话框,而是出来“文件已经存在,是否覆盖”再点击取消才退出。
这个是那个地方设置不对,请朋友们指正一下,谢谢!

解决方案 »

  1.   

    If Dir(strFileName) <> "" And strFileName <> "" Then '增加一个判断
      

  2.   


    on error resume Err
    CommonDialog1.ShowSave
      CommonDialog1.Filter = "(*.dat)|*.dat"
      strFileName = CommonDialog1.FileName
      If Dir(strFileName) <> "" Then
      If MsgBox("文件已经存在,是否覆盖?", vbInformation + vbOKCancel, "提示") = vbCancel Then
    Err: Exit Sub
      End If
      End If
      

  3.   

    Option ExplicitPrivate Sub Command1_Click()
            Dim strFileName As String
            
            On Error GoTo ErrTocjl
            
            CommonDialog1.ShowSave
            CommonDialog1.CancelError = True
            CommonDialog1.Filter = "(*.dat)|*.dat"
            strFileName = CommonDialog1.FileName
            If strFileName <> "" Then
               If Dir(strFileName) <> "" Then
                  If MsgBox("文件已经存在,是否覆盖?", vbInformation + vbOKCancel, "提示") = vbCancel Then
                     Exit Sub
                  Else
                     '覆盖的代码
                  End If
               End If
            End If
    ErrTocjl:
    End Sub