错误处理没有捕获运行时错误

时间:2013-02-16 05:44:33

标签: sharepoint vba excel-vba excel

我有一个从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.

1 个答案:

答案 0 :(得分:1)

很可能您的Visual Basic编辑器设置略有偏差。在选项 - > 常规 - > 错误捕获中,您需要检查Break on Unhandled Errors - 如果它是{{1} },你会得到你描述的行为。

enter image description here