sigil
sigil

Reputation: 9546

Error handling not catching run-time error

I have a procedure that checks out a workbook from Sharepoint and then opens the workbook. I noticed that it was generating a run-time error if the workbook has some missing required document properties, so I added some error trapping as follows:

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() is a function for inserting entries into an Access error log.

The error is being raised at Workbooks.CheckOut bkPath, but instead of going to the errHandler block, I get an error message box:

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

I want the error to be handled by closing the workbook if opened, and discarding the checkout, but I don't know how to catch the error in the errHandler code.

EDIT: If it helps, this is the warning message I get at the top of the workbook:

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

Upvotes: 2

Views: 792

Answers (1)

Peter Albert
Peter Albert

Reputation: 17475

Most likely your Visual Basic Editor settings are slightly off. In Options->General->Error Trapping, you need to check Break on Unhandled Errors - if it is Break on All Errors, you'll get the behavior you describe.

enter image description here

Upvotes: 1

Related Questions