logo
search
VBA & Macro Problems

How to Update DOCVARIABLE Fields in Word Headers with VBA

Huda QurayshiHuda Qurayshi Sep 30, 2026 869 views

Question details

The user needs a VBA macro solution to successfully update DOCVARIABLE fields that are placed inside the headers and footers of a document.

How to Update DOCVARIABLE Fields in Word Headers with VBA
Product
Microsoft Word / WPS Writer
Device & OS
not provided
Scenario
Running a macro to refresh document variables globally across an entire document, including sections outside the main text.
Observed behavior
Standard VBA commands like ActiveDocument.Range.Fields.Update only refresh fields in the main document body, leaving header and footer fields completely unchanged.
Before you start

Ensure you have saved a backup of your document and that macros are enabled in your Trust Center settings before running new VBA code.

Solution 1Recommended

Loop Through Document StoryRanges to Update All Fields

Iterate through all parts of the document, including headers, footers, and text boxes, to ensure every DOCVARIABLE field is updated properly.

Because word processors separate different areas of a document (such as headers, footers, and text boxes) into distinct 'StoryRanges', a simple Range.Fields.Update command only affects the main body. To resolve this, you must write a VBA loop that targets each StoryRange individually.

1
Open the VBA Editor

Press ALT + F11 to open the Visual Basic for Applications (VBA) Editor in your word processor.

2
Insert a New Module

Right-click your document project in the Project Explorer, select 'Insert', and click 'Module'.

3
Add the StoryRanges Loop Code

Create a sub-procedure and start a loop using 'For Each rngStory In ActiveDocument.StoryRanges'. This initializes the scan across all document sections.

4
Handle Linked Text Boxes

Inside the loop, add a nested 'Do While Not rngStory Is Nothing' loop using the 'NextStoryRange' property to ensure linked text boxes and shapes are caught.

5
Update the Fields

Insert the command 'rngStory.Fields.Update' inside your loop, then run the macro to refresh all DOCVARIABLE fields across headers, footers, and the main body.

Loop Through Document StoryRanges to Update All Fields
Updating the Table of Contents: Generated content such as a Table of Contents (TOC) may not update via StoryRanges. You will need a separate update command, such as ActiveDocument.TablesOfContents(1).Update, at the end of your macro.
Efficient Document Automation

Automate Documents Seamlessly with WPS Writer

WPS Office fully supports advanced VBA macros, allowing you to use the exact same StoryRanges code to update DOCVARIABLE fields in headers and footers. Enjoy a familiar developer environment with high performance.

  1. 1. Enable the Developer Tab: Open your document in WPS Writer, go to the top ribbon menu, and click on the 'Developer' tab to access macro tools.
  2. 2. Create a New Macro: Click 'Macros', name your macro, and click 'Create'. Paste your StoryRanges VBA code into the editor.
  3. 3. Run the Script: Click 'Run' or press F5 to execute the macro. WPS Writer will instantly update all DOCVARIABLE fields in your headers, footers, and document body.
Full VBA macro support for complex document automationSeamlessly compatible with Microsoft Word .docm and .dotm formatsLightweight, fast execution of scriptsFamiliar Developer tab and code editor interface
microsoft office alternative - wps office

Frequently Asked Questions

Why doesn't ActiveDocument.Range.Fields.Update affect my headers?

In the underlying object model, headers, footers, and text boxes are stored in separate 'StoryRanges' outside the main document body. The standard Range.Fields.Update command defaults to targeting only the main text story.

Do I need to update Tables of Contents separately in VBA?

Yes, generated tables like a Table of Contents (TOC) or Table of Figures may not fully refresh with a standard field update loop. You should update them explicitly using the ActiveDocument.TablesOfContents.Update method.

How do I ensure linked text boxes are also updated by my macro?

By calling the NextStoryRange property within a Do While loop, you can sequentially traverse through all linked shapes and text boxes to ensure any DOCVARIABLE fields contained within them are successfully refreshed.