Application.GetOpenFilename Method (Excel)[
^] displays the standard
Open dialog box
and gets a file name from the user without actually opening any files. When user cancel operation, this method returns
False
. So, below code is wrong!
Dim myfilepath As String
myfilepath = Application.GetOpenFilename()
If myfilepath = True Then
Open myfilepath For Input As #1
As MSDN documentation states, you have to check if returned value is not equal to
False
.
fileToOpen = Application _
.GetOpenFilename("Text Files (*.txt), *.txt")
If fileToOpen <> False Then
MsgBox "Open " & fileToOpen
End If
As to your code, i'd modify it this way:
Dim myfilepath As String
myfilepath = Application.GetOpenFilename("Text Files (*.txt), *.txt")
If myfilepath = CStr(False) Then Exit Sub
For further information, please see:
Programmatically Selecting Files in Excel for Windows and Excel for the Mac[
^]