excel - 应用程序退出指令后显示错误 91
问题描述
我已经构建了一个加载项,旨在从 www 下载。加载项将出现在用户计算机的“下载”文件夹中。
激活后,加载项活动的第一阶段是使用 SaveAs 将自身复制到用户的“/Microsoft/AddIns”文件夹中。然后“父”加载项自行关闭并退出 Excel。(在重新启动 Excel 时,“子”加载项将被加载并处于活动状态。)
代码是
Sub CheckInstall()
'Several lines of code before this.
'They have been tested and seem to work well.
MyNewfileNm = TestBase & GCSAPPNAME
If IsInstalled(MyNewfileNm) Then
Application.DisplayAlerts = False
ThisWorkBook.SaveCopyAs MyNewfileNm
Application.DisplayAlerts = True
MsgBox "We're done, and Excel will close." & Chr(13) & _
"On reopening you will find 'ACBA Mapping' loaded and active in the ADD-INS tab."
Excel.Application.Quit
ActiveWorkbook.Close False
Else
ThisWorkBook.SaveCopyAs MyNewfileNm
If ActiveWorkbook Is Nothing Then
Workbooks.Add
Set oAddIn = Application.AddIns.Add(MyNewfileNm, False)
oAddIn.Installed = True
Else
Set oAddIn = Application.AddIns.Add(MyNewfileNm, False)
oAddIn.Installed = True
End If
MsgBox "We're done, and Excel will close." & Chr(13) & _
"On reopening you will find 'ACBA Mapping' loaded and active in the ADD-INS tab."
Excel.Application.Quit
ActiveWorkbook.Close False
End If
End Sub
这会按预期处理和复制自身,但在 Application.Quit 完成之前,我收到错误代码 91。单击错误消息时,它只是恢复指令代码。结果完全符合预期。
但是,我必须要么解决生成错误消息的问题,要么抑制错误消息。暂时我也做不到。
我会很感激一个解决方案。
解决方案
事实证明,这对我来说是一个愚蠢的错误。下面修改后的代码运行没有错误。唯一的区别是如果主 IF 语句,则在第一部分中消除了关闭 ActiveWorkbook 的指令。那时没有可关闭的打开工作簿。
Sub Check Install()
'Several lines of code before this.
'They have been tested and seem to work well.
MyNewfileNm = TestBase & GCSAPPNAME
If IsInstalled(MyNewfileNm) Then
Application.DisplayAlerts = False
ThisWorkBook.SaveCopyAs MyNewfileNm
Application.DisplayAlerts = True
MsgBox "We're done, and Excel will close." & Chr(13) & _
"On reopening you will find the " & NewMappingVersion & " version of 'ACBA Mapping' loaded."
Excel.Application.Quit
' In principle we haven't initiated a Workbook so there is no need to close it.
' ActiveWorkbook.Close False
Else
ThisWorkBook.SaveCopyAs MyNewfileNm
If ActiveWorkbook Is Nothing Then
Workbooks.Add
Set oAddIn = Application.AddIns.Add(MyNewfileNm, False)
oAddIn.Installed = True
Else
' This shouldn't be necessary, but leave it for the time being.
Set oAddIn = Application.AddIns.Add(MyNewfileNm, False)
oAddIn.Installed = True
End If
MsgBox "We're done, and Excel will close." & Chr(13) & _
"On reopening you will find 'ACBA Mapping' version " & NewMappingVersion & " loaded and active in the ADD-INS tab."
Excel.Application.Quit
ActiveWorkbook.Close False
End If
End Sub
推荐阅读
- c# - 如何使用预先计算的签名在 c# 中创建 CADES-BES PKCS#7 签名消息?
- python - Python 更改电子邮件附件名称和扩展名
- asp.net-web-api - 来自服务的操作/控制器 URL
- r - R控制台捕获对象中的错误编码(西里尔编码)
- php - Wordpress - 自定义电子邮件发件人姓名被标记为未经身份验证
- angular - 使用 Angular6 无法正确获取日期选择器视图
- javascript - Ajax 调用动态加载的行
- github - Github 工作流不会在推送基于路径的过滤时触发
- python - os._exit(1) 或 os._exit(0) 不允许将日志推送到 AWS cloudwatch
- c# - 如何使用 Itext 7-dotnet 像 Acrobat 一样标记背景?