So, I am using the following code (modified a little by me), from http://www.cpearson.com/excel/browsefolder.aspx in order to get a path of a folder:
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
Everything runs smoothly, unless I press "escape", when I have to select the folder already. Then, I get the ugly vba message "execution of the code was interrupted" and that's all. If I press "debug", I go to line "If lID Then" and I can actually continue with F8, without a problem. But the "on error" does not catch it at all.
So my questions are: Main question: 1. What can I do, in order to press "escape" without breaking the whole excel app? Not so main question: 2. Why the on error does not catch this?
Edit: I also have these public declarations:
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
Working with Office 2010, 32 bits, Windows 7.