logo
search
VBA & Macro Problems

Fix VBA Macro Not Updating Every Text Box in Word

Muhammad TalhaMuhammad Talha Oct 10, 2026 870 views

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.

How to Use VBA to Add Text to Every Text Box in Word
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.
Before you start

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.

Solution 1Recommended

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.

1
Open the VBA Editor

Press Alt + F11 on your keyboard to open the Visual Basic for Applications (VBA) editor in your document.

2
Insert a New Module

Click on 'Insert' in the top menu bar, then select 'Module' to create a blank script window.

3
Enter the Corrected VBA Code

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.

4
Run the Macro

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.

Use a For Each Loop with msoTextBox Condition
Macro Execution Successful: The macro will now successfully process all text boxes by properly identifying them and iterating through the entire document without exiting prematurely.
Advanced Document Editing with WPS

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. 1. Open Your Document in WPS Writer: Launch WPS Office and open your macro-enabled document (.docm).
  2. 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. 3. Open the VBA Editor: Click on 'Visual Basic' or 'Macros' to open the integrated macro editor.
  4. 4. Run the Macro: Paste your corrected text box loop macro into a module and run it directly within WPS Writer.
Highly compatible with Microsoft Word (.docx, .docm) formatsSupports execution of standard VBA macro scriptsLightweight, fast, and easy to navigate Developer interfaceCompletely free to download for everyday productivity
microsoft office alternative - wps office

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.