首页 > 解决方案 > 应用程序退出指令后显示错误 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。单击错误消息时,它只是恢复指令代码。结果完全符合预期。

但是,我必须要么解决生成错误消息的问题,要么抑制错误消息。暂时我也做不到。

我会很感激一个解决方案。

标签: excelvba

解决方案


事实证明,这对我来说是一个愚蠢的错误。下面修改后的代码运行没有错误。唯一的区别是如果主 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

推荐阅读