John Boe
John Boe

Reputation: 3621

Is there something to suppress error in VBA?

In a Word I am running a macro which triggers Error dialog sometimes. In PHP there I am used to use @command syntax which causes that in case of error the error is not printed to the output. Is there something similar to prevent the VBA debugger stop working?

My code is

Documents.Add.Content.Paste

and I want to create code, which would test if the result is valid, without breaking the debugger and printing the error, In the case that it failed, I would create timer 1s, and try the command again.

Edit:

Current code:

On Error GoTo ErrHandler
Documents.Add.Content.Paste
If False Then
ErrHandler:
  ' Add a delay
  Dim tmpStart
  tmpStart = Timer
  Do
    DoEvents
  Loop While (tmpStart + 1) > Timer
  Documents.Add.Content.Paste
End If

Currently error happens at line #11: 4605 - property not available because clipboard is empty or damaged

Upvotes: 1

Views: 4349

Answers (1)

SPlatten
SPlatten

Reputation: 5760

In your sub-routine or function, insert as the first line:

    On Error Resume Next

Or if you want to handle the error:

    On Error Goto ErrHandler

    If False Then
    ErrHandler:
        MsgBox Err.Description
        Exit Sub ' Or Exit Function
    End If

Upvotes: 3

Related Questions