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.
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.
- 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. - 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. TypeOn Error Resume Next. This tells VBA to ignore any runtime error and move to the next line. - 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. - Check the Err object after the operation
Add anIf Err.Number <> 0 Thenblock 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 - Restore default error handling
After the risky section, addOn Error GoTo 0to 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.
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.
- Declare the error label at the start of the procedure
At the top of your macro, after theSuborFunctionline, addOn Error GoTo ErrorHandler. The label name can be anything, butErrorHandleris standard. - Write the main macro code normally
Include all the operations you need. If any line causes an error, execution jumps immediately to the label. - Add an Exit Sub or Exit Function before the label
PlaceExit SuborExit Functionjust before the error label. This prevents the error-handling code from running when no error occurs. - Write the error-handling block
TypeErrorHandler:on its own line. Below it, add code to handle the error. For example:ErrorHandler:
MsgBox "Error " & Err.Number & ": " & Err.Description, vbCritical
Resume Next
TheResume Nextstatement returns execution to the line immediately after the one that caused the error. Alternatively, useResumeto retry the failing line, orResume ExitHereto jump to a second label that handles cleanup. - Clear the error object
AddErr.Clearat 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.