Tocogroup
asked on
What error handling should I use for an Excel VBA GetOpenFileName method
Hi Experts,
I have an Excel application which displays the Open file dialog box. However, if I select the Cancel button my application falls over. What should I be testing for in this instance ? How do I code it ?
Thanks
Toco
I have an Excel application which displays the Open file dialog box. However, if I select the Cancel button my application falls over. What should I be testing for in this instance ? How do I code it ?
Thanks
Toco
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
here it is
Sub OpenFile()
' by ProfessorJimJam
FFF = MsgBox("Do you know where your file is located?", vbYesNo, "Do you know where your file is located")
'MsgBox (FFF)
If FFF = vbNo Then Exit Sub
If FFF = vbYes Then
With Application.FileDialog(msoFileDialogOpen)
.AllowMultiSelect = False
.Filters.Clear
.Filters.Add "Only EXCEL FILE *.xlsx can be selected", "*.xlsx", 1
.Show
For lngCount = 1 To .SelectedItems.Count
PathPath = .SelectedItems(lngCount)
Next lngCount
End With
If Len(PathPath) = 0 Then
FFF = MsgBox("You have CANCELLED selection of needed FILE", vbCritical, "- FOLLOW INSTRUCTION")
Exit Sub
'rest of your code
end sub
ASKER
Thank you for your suggestions, all.
on error goto next
then after the code line put this on error goto 0