开发者

Open workbook, exit sub on failure / abort

开发者 https://www.devze.com 2023-04-04 05:55 出处:网络
I\'m trying to open a woorkbook in the background in a macro. When the user exits the open file dialog, I want the program to quit, of course.

I'm trying to open a woorkbook in the background in a macro. When the user exits the open file dialog, I want the program to quit, of course.

But every attempt of doing so failed...

' Get the file to open
tempFile = Application.GetOpenFilename("Excel Files (*.xls), *.xls")

What I tried so far:

' Catch abort of the open file dialog
If IsEmpty(tempFile) Then
 开发者_StackOverflow中文版   End
End If
' Catch abort of the open file dialog
If IsEmpty(tempFile) Or Not tempFile Then
    End
End If
' Catch abort of the open file dialog
If IsEmpty(tempFile) Or Not CBool(tempFile) Then
    End
End If
' Catch abort of the open file dialog
If IsEmpty(tempFile) Or tempFile Like "false" Then
    End
End If

No matter what, I always get a "Type mismatch" error.


dim tempFile
tempFile = Application.GetOpenFilename("Excel Files (*.xls), *.xls")
if tempFile = False then
   'user cancelled the dialog. exit the code
else
   msgbox "User wants to open the file at : " & tempFile
end if


Also, be careful because if you do

dim tempFile as String

...which is probably the correct thing to do, then you have to do the check like this:

If tempFile = "False" Then

which goes against the grain, but works.

0

精彩评论

暂无评论...
验证码 换一张
取 消