Fix Word VBA Sub Stopping Mid-Run on Specific Document Operation
🔍 WiseChecker

Fix Word VBA Sub Stopping Mid-Run on Specific Document Operation

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.

ADVERTISEMENT

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

  1. Open the VBA Editor
    Press Alt+F11 in Word to open the Visual Basic Editor.
  2. Open Options
    Click Tools > Options. Go to the General tab.
  3. Set Error Trapping
    Select “Break on Unhandled Errors”. Click OK.
  4. 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

  1. Insert On Error Resume Next
    At the top of the Sub, add the line On Error Resume Next. This tells VBA to continue to the next line even if an error occurs.
  2. 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
  3. View the Immediate Window
    Press Ctrl+G to open the Immediate Window. Run the macro. The logged error appears there.
  4. Restore normal error handling
    After the test, remove the On Error Resume Next line or replace it with On Error GoTo 0.

Step 3: Check Document Protection

  1. Open the Review tab
    In Word, go to Review > Protect > Restrict Editing.
  2. Review protection settings
    If the task pane shows “Your document is protected”, click Stop Protection. Enter the password if prompted.
  3. Check for Mark as Final
    Go to File > Info > Protect Document. If “Mark as Final” shows, click it to remove the final status.
  4. Run the macro again
    If the Sub no longer stops, protection was the cause.

Step 4: Disable Add-Ins Temporarily

  1. 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.
  2. Run the macro
    If the Sub completes without stopping, an add-in is interfering.
  3. 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

  1. Break the operation into smaller steps
    If the failing line is Selection.Find.Execute Replace:=wdReplaceAll, split it into a Find and then a Replace.
  2. Use explicit object references
    Replace Selection with a defined Range object:
    Dim rng As Range
    Set rng = ActiveDocument.Content
    rng.Find.Execute FindText:="old", ReplaceWith:="new", Replace:=wdReplaceAll
  3. Add a DoEvents call
    Insert DoEvents before the failing operation to allow Word to process pending events.

ADVERTISEMENT

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.

ADVERTISEMENT