【问题标题】:Cancel Button On Open File Dialog Doesn't Close Window For Visual Basic打开文件对话框上的取消按钮不会关闭 Visual Basic 的窗口
【发布时间】:2016-12-24 11:13:04
【问题描述】:

我正在编写一个窗口,要求提示用户保存文件。它关闭并询问他们是否要覆盖文件,除非我按下取消它仍然卡在 while 循环中。有谁知道取消/关闭窗口的代码?

Private Sub btn_Browse1_Click()

  Dim strFilter As String
Dim strOutputFileName As String, compareFileName As String, response As Integer, fileSet As Boolean

strFilter = ahtAddFilterItem(strFilter, "Excel Files (*.xlsx)", "*.xlsx")

fileSet = False
While fileSet = False
    strOutputFileName = ahtCommonFileOpenSave( _
                        Filter:=strFilter, _
                        OpenFile:=False, _
                        DialogTitle:="Choose an image file...", _
                        Flags:=ahtOFN_HIDEREADONLY)


        If Len(strOutputFileName) > 0 Then
        compareFileName = Dir(strOutputFileName)
        If compareFileName <> "" Then 'find exist file with the same name
            response = MsgBox("The current file name already exists. Do you want to replace the file " & strOutputFileName & " with the current one?", vbYesNo)
            If response = vbYes Then
                fileSet = True      'replace old file
                Kill strOutputFileName
            Else
                fileSet = False
            End If
        Else 'no file exists with the same name
            fileSet = True
        End If

    End If
Wend
Me.txt_File_Level1.Value = strOutputFileName

【问题讨论】:

  • 我建议您更改标签以包含 excel-vba 并删除 vb.net

标签: excel vba ms-access openfiledialog cancel-button


【解决方案1】:

你的文件集在你的 else 子句中等于 False

If response = vbYes Then
    fileSet = True
Else
    fileSet = False
End If

我会先用 do/while 循环替换,因为如果我没记错的话,你只能用 GOTO 语句提前退出。

Dim strFilter As String
Dim strOutputFileName As String, compareFileName As String, response As Integer, fileSet As Boolean

strFilter = ahtAddFilterItem(strFilter, "Excel Files (*.xlsx)", "*.xlsx")

fileSet = False
Do While fileSet = False
    strOutputFileName = ahtCommonFileOpenSave( _
                    Filter:=strFilter, _
                    OpenFile:=False, _
                    DialogTitle:="Choose an image file...", _
                    Flags:=ahtOFN_HIDEREADONLY)


    If Len(strOutputFileName) > 0 Then
        compareFileName = Dir(strOutputFileName)
        If compareFileName <> "" Then 'find exist file with the same name
            response = MsgBox("The current file name already exists. Do you want to replace the file " & strOutputFileName & " with the current one?", vbYesNo)
            If response = vbYes Then
                fileSet = True      'replace old file
                Kill strOutputFileName
            Else
                fileSet = False
                Exit Do
            End If
        Else 'no file exists with the same name
            fileSet = True
        End If
    End If
Loop
Me.txt_File_Level1.Value = strOutputFileName

【讨论】:

  • 嗯...在我按下取消按钮后似乎仍然再次弹出。抱歉,我是个新手。
  • 即使在替换你的while循环时?你是在 Visual Studio 中还是在 access/excel 中使用 vba?
  • AccessExcel 中的 VBA
  • 我无法进行更多测试,因为我附近没有办公室,但我认为更改循环应该可以解决您的问题,因为当您单击 Cancel 时您永远不会退出循环response = vbYes 的 else 子句)因为 fileSet 仍设置为 False。如果您想查看,我已经更新了我的答案。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2014-08-01
  • 1970-01-01
  • 1970-01-01
  • 2015-05-09
  • 1970-01-01
  • 2013-03-25
相关资源
最近更新 更多