Fix VBA Macro Not Updating Every Text Box in Word
Question details
The user needs a VBA macro to add specific text tags before and after the content of every text box in a document, but the current code fails to loop through all of them.

- Product
- Word
- Device & OS
- not provided
- Scenario
- Running a VBA macro to batch insert text into multiple text boxes across a document.
- Observed behavior
- The macro only inserts the text into the first rectangle-shaped text box and stops, instead of iterating through every text box in the document.
Ensure you have a backup of your document before running VBA macros that modify text in bulk, and verify that the Developer tab is enabled to access the Visual Basic Editor.
Use a For Each Loop with msoTextBox Condition
Fix the macro by iterating through the entire Shapes collection without an Exit For statement, and explicitly checking if the shape type is msoTextBox.
The most common reason a macro stops after the first text box is an 'Exit For' command inside the loop or incorrectly filtering shape types by 'Rectangle' instead of 'msoTextBox'. By iterating through all shapes and verifying the correct type, the macro will successfully update every text box.
Press Alt + F11 on your keyboard to open the Visual Basic for Applications (VBA) editor in your document.
Click on 'Insert' in the top menu bar, then select 'Module' to create a blank script window.
Copy and paste the following loop: Sub AddTextToTextBoxes() Dim oShape As Shape: For Each oShape In ActiveDocument.Shapes: If oShape.Type = msoTextBox Then: oShape.TextFrame.TextRange.InsertBefore "[BeginTextbox]": oShape.TextFrame.TextRange.InsertAfter "[EndTextbox]": End If: Next oShape: End Sub.
Press F5 or click the Run button (green triangle) to execute the code. The macro will now check every shape and add the text only to actual text boxes.

Iterate Through Shapes in Reverse Order
If your macro modifies the document structure in a way that shifts shape indexing, iterating backwards ensures all text boxes are processed.
Run VBA Macros Seamlessly in WPS Office
WPS Office provides excellent compatibility with Microsoft Office macro scripts. You can easily open your Word document in WPS Writer and run your VBA macro to update all text boxes without needing to rewrite your code.
- 1. Open Your Document in WPS Writer: Launch WPS Office and open your macro-enabled document (.docm).
- 2. Access the Developer Tab: Navigate to the Developer tab on the top ribbon. If it is not visible, enable it in the WPS Options menu.
- 3. Open the VBA Editor: Click on 'Visual Basic' or 'Macros' to open the integrated macro editor.
- 4. Run the Macro: Paste your corrected text box loop macro into a module and run it directly within WPS Writer.

Frequently Asked Questions
Why does my Word macro only update the first text box?
This usually happens if your VBA code contains an 'Exit For' statement inside the loop, which stops execution after the first match, or if it specifically checks for a single 'Rectangle' shape type instead of looping through all 'msoTextBox' types.
How do I identify if a shape is a text box in VBA?
You can check the shape's type using the 'Type' property. If 'Shape.Type = msoTextBox', then the shape is a standard text box. This avoids confusing text boxes with regular rectangle shapes that happen to contain text.
Can I use Content Controls instead of Text Boxes in Word?
Yes. Text boxes can sometimes cause formatting and macro iteration issues. Using Rich Text or Plain Text Content Controls placed inside borderless table cells is a more robust alternative for structured document data.




