VBA & Macro Problems

Prevent Word VBA Crashes When Editing Text with Comments

Steven ParkerSteven Parker Nov 3, 2026 872 views

Question details

A Word VBA macro that adds quotation marks crashes or freezes when the selected text contains a comment.

Prevent Word VBA Crashes When Editing Text with Comments
Product
Word
Device & OS
not provided
Scenario
Running a VBA macro to edit text or add quotation marks around a selection that includes or ends with a document comment.
Observed behavior
The macro crashes or gets stuck in an infinite loop, particularly when the MoveEndWhile method reaches the endpoint of a comment range.
Before you start

Before modifying your VBA script, ensure you have saved a backup of your current document to prevent any data loss in case the macro enters an infinite loop during testing.

Solution 1Recommended

Use a Duplicated Range and Do Until Loop

Replace the standard Selection object with a duplicated Range and utilize a Do Until loop instead of MoveEndWhile to safely bypass comment boundaries without crashing.

The MoveEndWhile method often fails and creates an infinite loop when it encounters the end of a comment scope in Microsoft Word. Operating directly on the UI Selection can also exacerbate freezing.

By switching to an invisible Range object and manually calculating the endpoint using a controlled backward loop, you can trim unwanted spaces and insert characters safely.

1
Duplicate the Selection to a Range

In your VBA editor, declare a new Range variable (e.g., Dim rng As Range) and set it to duplicate your current selection using 'Set rng = Selection.Range'.

2
Remove the MoveEndWhile Method

Locate the 'MoveEndWhile' function in your script that attempts to trim trailing spaces or paragraph marks, and delete or comment out this line to prevent the infinite loop.

3
Implement a Do Until Loop

Create a 'Do Until' loop that moves the range end backward by one character at a time as long as the final character is a space or a paragraph mark. This manually trims the text safely.

4
Check Comment Endpoints

Within your script logic, ensure you check if a comment scope ends exactly at the adjusted endpoint, effectively handling multiple comments if they fall within the boundaries.

5
Insert the Quotation Marks

Once the range is properly adjusted and trimmed, use 'rng.InsertBefore' and 'rng.InsertAfter' to place the quotation marks, then reset the formatting on the first and last characters if needed.

Use a Duplicated Range and Do Until Loop
Macro Stability Improved: Using a Range instead of Selection not only prevents comment-related crashes but also makes your macro run significantly faster by avoiding UI updates during execution.
VBA Macro Debugging

Debug and Fix VBA Macros Safely in WPS Writer

You can resolve infinite loop crashes by editing your VBA macro directly within WPS Writer. Using its built-in macro editor, you can efficiently switch to a Range-based approach to bypass comment boundaries with smooth performance.

  1. 1. Open Your Document in WPS Writer: Launch WPS Office and open the document containing the macro and comments you wish to edit.
  2. 2. Access the Developer Tools: Navigate to the 'Tools' tab on the top ribbon and click on 'Developer' to reveal the VBA macro tools.
  3. 3. Edit the VBA Script: Click on 'Macros', select your script from the list, and click 'Edit' to open the VBA Editor where you can safely implement the Do Until loop.
Fully compatible with Microsoft Word documents and VBA scriptsBuilt-in advanced developer tools and Macro EditorLightweight design prevents heavy freezes during macro testing
microsoft office alternative - wps office

Frequently Asked Questions

Why does MoveEndWhile cause a crash when a comment is present?

The MoveEndWhile method gets trapped in an infinite loop because the invisible endpoint of a comment range prevents the selection from moving past it properly, causing the application to hang.

What is the difference between Range and Selection in VBA?

Selection represents the currently highlighted text in the user interface, which triggers visual updates and can lock up. A Range defines an invisible segment of text in the document's background, making it much more stable and faster for complex macro operations.

How do I detect a comment within a VBA text range?

You can use the 'Range.Comments.Count' property in your VBA script to check if any comments exist within a specific range before attempting to modify or loop through the text.