在VBA中浏览文件夹时,由“退出button”生成的错误消息

所以,我使用下面的代码(由我修改一下),从http://www.cpearson.com/excel/browsefolder.aspx为了得到一个文件夹的path:

Function str_BrowseFolder(Optional ByVal DialogTitle As String) As String On Error GoTo str_BrowseFolder_Error ''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''' ' BrowseFolder ' This displays the standard Windows Browse Folder dialog. It returns ' the complete path name of the selected folder or vbNullString if the ' user cancelled. ''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''' If DialogTitle = vbNullString Then DialogTitle = "Select A Folder" End If Dim uBrowseInfo As BROWSEINFO Dim szBuffer As String Dim lID As Long Dim lRet As Long With uBrowseInfo .hOwner = 0 .pidlRoot = 0 .pszDisplayName = String$(MAX_PATH, vbNullChar) .lpszINSTRUCTIONS = DialogTitle .ulFlags = BIF_RETURNONLYFSDIRS ' + BIF_USENEWUI .lpfn = 0 End With szBuffer = String$(MAX_PATH, vbNullChar) lID = SHBrowseForFolderA(uBrowseInfo) If lID Then ''' Retrieve the path string. lRet = SHGetPathFromIDListA(lID, szBuffer) If lRet Then str_BrowseFolder = Left$(szBuffer, InStr(szBuffer, vbNullChar) - 1) End If End If On Error GoTo 0 Exit Function str_BrowseFolder_Error: MsgBox "Error " & Err.Number & " (" & Err.Description & ") in procedure str_BrowseFolder of Function mod_Browse" End Function 

一切运行顺利,除非我按“逃生”,当我不得不select文件夹已经。 然后,我得到丑陋的vba消息“代码的执行被中断”,就这些。 如果我按“debugging”,我去“如果LID然后”,我可以继续F8,没有问题。 但是“错误”根本就没有理解。

所以我的问题是:主要问题:1.我能做什么,为了按“逃脱”而不打破整个Excel应用程序? 不是这么主要的问题:2.为什么错误不能抓住这个?

编辑:我也有这些公开声明:

 Private Declare Function SHGetPathFromIDListA Lib "shell32.dll" (ByVal pidl As Long, ByVal pszBuffer As String) As Long Private Declare Function SHBrowseForFolderA Lib "shell32.dll" (lpBrowseInfo As BROWSEINFO) As Long 

使用Office 2010,32位Windows 7。

CpPearson确实是一个很棒的网站,但是,为什么这么复杂呢?

 Sub myPathForFolder() Dim FolderSelected As String FolderSelected = GetFolder(Environ("USERPROFILE") & "\Documents") If FolderSelected <> "" Then ' 1. If FolderSelected <> "" 'If not, it would mean user didn't select folder or pressed cancel 'your stuff End If End Sub Function GetFolder(InitialLocation As String) As String Dim FolderDialog As FileDialog Dim SelectedFolder As String Set FolderDialog = Excel.Application.FileDialog(msoFileDialogFolderPicker) With FolderDialog .Title = "My Title For Dialog" .AllowMultiSelect = False .InitialFileName = InitialLocation If .Show <> -1 Then GoTo NextCode SelectedFolder = .SelectedItems(1) End With NextCode: GetFolder = SelectedFolder Set FolderDialog = Nothing End Function 

编辑:

没有真正的build议,但是,这可能工作:

 Sub isGettingValue() Application.EnableCancelKey = xlDisabled myPath = str_BrowseFolder("DummyTitle") Application.EnableCancelKey = xlInterrupt 

对于第二个问题,错误没有得到它,因为它本身不是一个错误,停止VBA是由用户引起的。

所以,最后,我的最终代码如下所示:

 Private Declare Function SHGetPathFromIDListA Lib "shell32.dll" (ByVal pidl As Long, ByVal pszBuffer As String) As Long Private Declare Function SHBrowseForFolderA Lib "shell32.dll" (lpBrowseInfo As BROWSEINFO) As Long Private Const MAX_PATH = 260 Function str_BrowseFolder(Optional ByVal DialogTitle As String) As String On Error GoTo str_BrowseFolder_Error ''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''' ' BrowseFolder ' This displays the standard Windows Browse Folder dialog. It returns ' the complete path name of the selected folder or vbNullString if the ' user cancelled. ''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''' Application.EnableCancelKey = xlDisabled If DialogTitle = vbNullString Then DialogTitle = "Select A Folder" End If Dim uBrowseInfo As BROWSEINFO Dim szBuffer As String Dim lID As Long Dim lRet As Long With uBrowseInfo .hOwner = 0 .pidlRoot = 0 .pszDisplayName = String$(MAX_PATH, vbNullChar) .lpszINSTRUCTIONS = DialogTitle .ulFlags = BIF_RETURNONLYFSDIRS ' + BIF_USENEWUI .lpfn = 0 End With szBuffer = String$(MAX_PATH, vbNullChar) lID = SHBrowseForFolderA(uBrowseInfo) If lID Then ''' Retrieve the path string. lRet = SHGetPathFromIDListA(lID, szBuffer) If lRet Then str_BrowseFolder = Left$(szBuffer, InStr(szBuffer, vbNullChar) - 1) End If End If Application.EnableCancelKey = xlInterrupt On Error GoTo 0 Exit Function str_BrowseFolder_Error: MsgBox "Error " & Err.Number & " (" & Err.Description & ") in procedure str_BrowseFolder of Function mod_Browse" End Function