How to Update DOCVARIABLE Fields in Word Headers with VBA
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.

- 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.
Ensure you have saved a backup of your document and that macros are enabled in your Trust Center settings before running new VBA code.
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.
Press ALT + F11 to open the Visual Basic for Applications (VBA) Editor in your word processor.
Right-click your document project in the Project Explorer, select 'Insert', and click 'Module'.
Create a sub-procedure and start a loop using 'For Each rngStory In ActiveDocument.StoryRanges'. This initializes the scan across all document sections.
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.
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.

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. 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. Create a New Macro: Click 'Macros', name your macro, and click 'Create'. Paste your StoryRanges VBA code into the editor.
- 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.

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.




