Link to home
Start Free TrialLog in
Avatar of Tocogroup
TocogroupFlag for United Kingdom of Great Britain and Northern Ireland

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
Avatar of Professor J
Professor J

put this before

on error goto next

then after the code line put this  on error goto 0
ASKER CERTIFIED SOLUTION
Avatar of Haris Dulic
Haris Dulic
Flag of Austria image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
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

Open in new window

Avatar of Tocogroup

ASKER

Thank you for your suggestions, all.