You run a VBA macro in Word and it stops partway through, always at the same document operation such as a FindReplace, a TableSort, or a Range.Insert. This symptom points to a runtime error that the default error handler does not catch, or to a condition that triggers Word’s built-in protection. The root cause is usually an unhandled error, a protected document structure, or a conflict between VBA and a background add-in. This article explains the exact technical reasons why a Sub halts mid-run and provides step-by-step methods to isolate and fix the failing operation.
Key Takeaways: Stop VBA Sub Failures on Specific Document Operations
- VBA Editor > Tools > Options > General > Break on Unhandled Errors: Forces the debugger to stop at the exact line that fails, revealing the error number and description.
- On Error Resume Next / On Error GoTo 0: Temporarily suppresses runtime errors so the macro continues; add error logging to identify the failing operation.
- File > Options > Trust Center > Trust Center Settings > Macro Settings > Disable all macros with notification: Temporarily turn off add-ins that might intercept document operations and cause a halt.
Why a VBA Sub Stops Mid-Run on a Specific Operation
When a VBA Sub stops at a specific line such as Selection.Find.Execute or ActiveDocument.Tables(1).Sort, the immediate cause is a runtime error that the VBA runtime cannot handle. Without an active error handler, VBA displays a message box and halts execution. Common runtime errors at these points include:
Object Required Errors (Error 424)
This occurs when the code references an object that no longer exists. For example, if the macro deletes a table and then tries to sort it, the table object is gone. The Sub stops at the sorting line.
Method or Data Member Not Found (Error 438)
This happens when the code calls a method that the current object does not support. For instance, calling .Sort on a Range that spans multiple table cells may fail if the range is not a valid Table object.
Permission Denied (Error 70) or Document Protected Errors
If the document has editing restrictions, form fields, or is marked as final, certain operations like Range.Insert or Find.Execute with replace will throw an error and stop the macro.
Out of Memory or System Resources (Error 7)
Large documents with many tracked changes or embedded images can exhaust available memory during a FindReplace operation, causing the Sub to stop.
Steps to Diagnose and Fix the Halting Sub
Follow these steps in order. Each step isolates a different possible cause.
Step 1: Enable Break on Unhandled Errors
- Open the VBA Editor
Press Alt+F11 in Word to open the Visual Basic Editor. - Open Options
Click Tools > Options. Go to the General tab. - Set Error Trapping
Select “Break on Unhandled Errors”. Click OK. - Run the macro again
When the Sub stops, the debugger highlights the failing line. Note the error number and description in the message box.
Step 2: Add a Temporary Error Handler
- Insert On Error Resume Next
At the top of the Sub, add the lineOn Error Resume Next. This tells VBA to continue to the next line even if an error occurs. - Log the Error
After the operation that fails, add this code to capture the error:If Err.Number <> 0 Then
Debug.Print "Error " & Err.Number & ": " & Err.Description
Err.Clear
End If - View the Immediate Window
Press Ctrl+G to open the Immediate Window. Run the macro. The logged error appears there. - Restore normal error handling
After the test, remove theOn Error Resume Nextline or replace it withOn Error GoTo 0.
Step 3: Check Document Protection
- Open the Review tab
In Word, go to Review > Protect > Restrict Editing. - Review protection settings
If the task pane shows “Your document is protected”, click Stop Protection. Enter the password if prompted. - Check for Mark as Final
Go to File > Info > Protect Document. If “Mark as Final” shows, click it to remove the final status. - Run the macro again
If the Sub no longer stops, protection was the cause.
Step 4: Disable Add-Ins Temporarily
- Open Word Safe Mode
Hold Ctrl while starting Word. Click Yes when asked to start in Safe Mode. In Safe Mode, all add-ins are disabled. - Run the macro
If the Sub completes without stopping, an add-in is interfering. - Identify the problematic add-in
Exit Safe Mode. Go to File > Options > Add-Ins. Disable add-ins one by one, restarting Word each time, until the macro works.
Step 5: Simplify the Failing Operation
- Break the operation into smaller steps
If the failing line isSelection.Find.Execute Replace:=wdReplaceAll, split it into a Find and then a Replace. - Use explicit object references
ReplaceSelectionwith a defined Range object:Dim rng As Range
Set rng = ActiveDocument.Content
rng.Find.Execute FindText:="old", ReplaceWith:="new", Replace:=wdReplaceAll - Add a DoEvents call
InsertDoEventsbefore the failing operation to allow Word to process pending events.
If the Sub Still Stops After the Main Fixes
“Word VBA Sub stops on Find.Execute but works manually”
The Find object may be targeting a collapsed range. Set the range to a valid area before calling Execute. For example, rng.Start = 0: rng.End = ActiveDocument.Content.End.
“Macro stops on Table.Sort with error 4605”
This error indicates the method is not valid for the object. Ensure the table has more than one row and that the range includes the entire table. Use ActiveDocument.Tables(1).Range instead of Selection.
“VBA stops on Range.Insert with error 4198”
Command failed because the document contains tracked changes. Accept all changes before running the macro: ActiveDocument.AcceptAll.
“Sub stops silently without error message”
This often happens when the macro calls End or Stop inside a conditional block. Search the code for End or Stop statements and comment them out temporarily.
Debugging Methods: VBA Editor vs Immediate Window vs Log File
| Item | VBA Editor Break Mode | Immediate Window Debug.Print | Log File (Write to Text File) |
|---|---|---|---|
| Setup effort | None (built-in) | Add Debug.Print lines | Add Open/Print/Close code |
| Shows exact line | Yes, highlighted | No, only logged messages | No, only logged messages |
| Works without user interaction | No, stops at error | Yes, continues | Yes, continues |
| Best for | Immediate crash at known line | Intermittent errors | Production macros where you cannot watch |
You can now identify the exact runtime error that stops your VBA Sub and fix the specific document operation causing it. Start by enabling Break on Unhandled Errors to see the error number. Then apply the error handler or protection check that matches the symptom. For ongoing macro maintenance, add a simple error handler with logging to every Sub that touches document content. This prevents silent halts and gives you a record of which operation failed.