2

我有一个从 Sharepoint 签出工作簿然后打开工作簿的程序。我注意到如果工作簿缺少一些必需的文档属性,它会生成运行时错误,因此我添加了一些错误捕获,如下所示:

Public Function getWorkbook(bkPath As String) As Workbook

Dim bk As Workbook
Dim response As Boolean

secAutomation = Application.AutomationSecurity

On Error GoTo errHandler

If Workbooks.CanCheckOut(bkPath) Then
    Workbooks.CheckOut bkPath
    Application.AutomationSecurity = msoAutomationSecurityForceDisable
    Application.EnableEvents = False
    Application.DisplayAlerts = False
    Set bk = Workbooks.Open(bkPath, False, False)
    Application.DisplayAlerts = True
    Application.EnableEvents = True
    Application.AutomationSecurity = secAutomation
    If bk Is Nothing Then
        response = dbo_global.setStatus("error", failedToOpen)
    End If
Else
    response = dbo_global.setStatus("error", checkedout)
    Set bk = Nothing
End If
Set getWorkbook = bk
Exit Function

errHandler:
Application.DisplayAlerts = True
Application.EnableEvents = True
Application.AutomationSecurity = secAutomation
If Not bk Is Nothing Then
    bk.Close False
    Set bk = Nothing
End If
response = dbo_global.setStatus("error", checkoutProblem)

End Function

dbo_global.setStatus()是用于将条目插入 Access 错误日志的函数。

错误是在 处引发的Workbooks.CheckOut bkPath,但我没有转到该errHandler块,而是收到一个错误消息框:

Run-time error '-2147467259 (80004005)':    
This document cannot be checked in.

我希望通过关闭打开的工作簿并丢弃结帐来处理错误,但我不知道如何在errHandler代码中捕获错误。

编辑:如果有帮助,这是我在工作簿顶部收到的警告消息:

Required Properties
To save to the server, correct the invalid or missing required properties.
4

1 回答 1

1

很可能您的 Visual Basic 编辑器设置稍有偏差。在Options -> General -> Error Trapping中,您需要检查Break on Unhandled Errors- 如果是Break on All Errors,您将获得您描述的行为。

在此处输入图像描述

于 2013-02-16T22:34:23.323 回答