首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >打开工作簿,失败时退出sub /中止

打开工作簿,失败时退出sub /中止
EN

Stack Overflow用户
提问于 2011-09-13 14:39:34
回答 2查看 1.1K关注 0票数 0

我正试图在后台的宏中打开一个woorkbook。当用户退出打开文件对话框时,我当然希望程序退出。

但每次尝试都失败了.

代码语言:javascript
复制
' Get the file to open
tempFile = Application.GetOpenFilename("Excel Files (*.xls), *.xls")

到目前为止,我尝试了以下内容:

代码语言:javascript
复制
' Catch abort of the open file dialog
If IsEmpty(tempFile) Then
    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

无论如何,我总是得到一个“类型不匹配”的错误。

EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2011-09-13 14:58:44

代码语言:javascript
复制
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
票数 4
EN

Stack Overflow用户

发布于 2011-09-13 15:59:25

另外,要小心,因为如果你这样做了

代码语言:javascript
复制
dim tempFile as String

...which可能是正确的选择,然后你必须像这样做检查:

代码语言:javascript
复制
If tempFile = "False" Then

这与本意相反,但却是可行的。

票数 -2
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/7397776

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档