How to Trap Word VBA Errors Without Halting the Macro Mid-Operation
🔍 WiseChecker

How to Trap Word VBA Errors Without Halting the Macro Mid-Operation

When a Word VBA macro encounters a runtime error, it stops execution and displays a default error dialog. This halts the operation, often leaving the document in an incomplete or corrupted state. The cause is the default error handling behavior in VBA, which treats any unhandled error as a critical failure. This article explains how to use structured error trapping with the On Error Resume Next and On Error GoTo statements to keep your macro running smoothly.

Key Takeaways: Trap Word VBA Errors Gracefully

  • On Error Resume Next: Instructs VBA to ignore a runtime error and continue with the next line of code, ideal for non-critical operations.
  • On Error GoTo [Label]: Sends execution to a labeled error-handling routine when an error occurs, allowing you to log or fix the issue.
  • Err Object (Err.Number, Err.Description, Err.Clear): Provides details about the last error; use Err.Clear after handling to reset the error state.

ADVERTISEMENT

How VBA Error Handling Works in Word Macros

VBA, the macro language for Word, uses a sequential execution model. When an error occurs on a line, the default behavior is to stop the macro and show a dialog box with the error number and description. This is controlled by the VBA runtime’s built-in error handler. To change this behavior, you insert specific error-handling statements at the start of your procedure or before the risky code block.

The two primary methods are inline error suppression with On Error Resume Next and structured error branching with On Error GoTo. Each serves a different purpose. On Error Resume Next is best when you expect occasional errors that do not affect the overall outcome, such as a missing bookmark or a protected range. On Error GoTo is better when you need to log the error, clean up resources, or exit the procedure safely.

After an error is trapped, you must check the Err object to see if an error occurred. The Err.Number property returns the error code, and Err.Description returns the message. After handling, call Err.Clear to reset the error state. If you do not clear the error, subsequent On Error Resume Next calls will still see the old error.

Steps to Implement On Error Resume Next for Non-Critical Operations

This method is ideal for operations that might fail but where the failure is acceptable, such as deleting a bookmark that may not exist or applying formatting to a selection that might be empty.

  1. Open the VBA editor
    Press Alt+F11 in Word to open the Microsoft Visual Basic for Applications editor. Locate the module containing your macro in the Project Explorer pane.
  2. Insert the On Error statement before the risky code
    Place the cursor at the beginning of the procedure or just before the line that might fail. Type On Error Resume Next. This tells VBA to ignore any runtime error and move to the next line.
  3. Write the operation that may fail
    For example, to delete a bookmark without crashing if it does not exist: ActiveDocument.Bookmarks("Temp").Delete. If the bookmark is missing, the macro continues.
  4. Check the Err object after the operation
    Add an If Err.Number <> 0 Then block to optionally log or ignore the error. For example:
    If Err.Number <> 0 Then
    Debug.Print "Error " & Err.Number & ": " & Err.Description
    Err.Clear
    End If
  5. Restore default error handling
    After the risky section, add On Error GoTo 0 to revert to the default error handling for the rest of the macro. This ensures that unexpected errors later in the code are not silently ignored.

ADVERTISEMENT

Steps to Implement On Error GoTo for Centralized Error Handling

Use this method when you need to perform cleanup actions or exit the macro gracefully after an error occurs. It centralizes all error logic in one labeled section.

  1. Declare the error label at the start of the procedure
    At the top of your macro, after the Sub or Function line, add On Error GoTo ErrorHandler. The label name can be anything, but ErrorHandler is standard.
  2. Write the main macro code normally
    Include all the operations you need. If any line causes an error, execution jumps immediately to the label.
  3. Add an Exit Sub or Exit Function before the label
    Place Exit Sub or Exit Function just before the error label. This prevents the error-handling code from running when no error occurs.
  4. Write the error-handling block
    Type ErrorHandler: on its own line. Below it, add code to handle the error. For example:
    ErrorHandler:
    MsgBox "Error " & Err.Number & ": " & Err.Description, vbCritical
    Resume Next

    The Resume Next statement returns execution to the line immediately after the one that caused the error. Alternatively, use Resume to retry the failing line, or Resume ExitHere to jump to a second label that handles cleanup.
  5. Clear the error object
    Add Err.Clear at the end of the error handler to reset the error state before the macro continues or ends.

Common Trapping Mistakes and How to Avoid Them

Forgetting to restore default error handling after On Error Resume Next

If you do not add On Error GoTo 0 after the risky section, the entire rest of the macro runs with errors suppressed. This can hide bugs and make debugging impossible. Always restore default handling as soon as the risky operation is complete.

Using Resume without a valid label after On Error GoTo

The Resume statement can only be used inside an active error handler. If you call Resume without a label, or if the label is misspelled, VBA raises a new error. Ensure the label name matches exactly and that the handler is reachable.

Not clearing the Err object before the next error check

If you check Err.Number after an operation but do not call Err.Clear, the next On Error Resume Next block will still see the old error. This causes false positives in your error checks. Always clear the object after handling.

On Error Resume Next vs On Error GoTo: Key Differences

Item On Error Resume Next On Error GoTo
Error behavior Ignores error and continues on next line Jumps to a labeled handler
Best use case Non-critical, expected failures Critical failures needing cleanup or logging
Error visibility Must check Err object manually Handler runs automatically on error
Restoration needed Yes, use On Error GoTo 0 Not required, but handler must exit properly
Resume options Not applicable Resume, Resume Next, Resume [label]

You now have two reliable methods to trap VBA errors in Word without halting your macro. Start by applying On Error Resume Next to simple, non-critical operations to prevent interruptions. For more complex macros that require cleanup, use On Error GoTo with a dedicated handler. Test your macros in a copy of the document first, and always check the Err object after inline error suppression. One advanced tip: combine both methods by using On Error Resume Next for individual operations inside a loop, and wrap the entire loop in an On Error GoTo handler to catch unexpected fatal errors.

ADVERTISEMENT