How to Replace Bookmark Text with Document Variables in Word VBA
Question details
The user needs a Word VBA procedure to replace a bookmark's text with a value entered via a UserForm using a document variable.

- Product
- Word
- Device & OS
- not provided
- Scenario
- Automating Word documents using VBA and UserForms to update hidden document variables instead of directly modifying bookmark ranges.
- Observed behavior
- Attempting to assign a Range object to a Document Variable results in an error because document variables do not support the Range property.
Ensure your Word document contains the correct bookmarks and that you have enabled the Developer tab to access the VBA editor.
Use the Value Property to Update Document Variables
Assign the bookmark's text or UserForm value to the document variable using its .Value property, then update the document fields.
Document variables are designed to store hidden string data within a Word document's backend. Unlike Bookmarks, they are not physical elements on the page, which means they do not have a Range property. To read or update a document variable using VBA, you must explicitly call its Value property.
Press Alt + F11 in Word to open the Microsoft Visual Basic for Applications editor.
Find the module or UserForm code where you are handling the document variables and bookmarks.
Use the syntax ActiveDocument.Variables("YourVariableName").Value to assign the new text. To pull text from a bookmark, write: ActiveDocument.Variables("bmprogramabbrv").Value = ActiveDocument.Bookmarks("bmprogramabbrv").Range.Text.
Add the command ActiveDocument.Fields.Update right after assigning the value. This ensures that any DOCVARIABLE fields embedded in your document refresh to display the newly assigned text.

Looking for an Easier Way to Manage Documents? Try WPS Office
If you are struggling with complex VBA scripts and document variables in Microsoft Word, consider switching to WPS Office. It provides an intuitive interface, excellent compatibility with Microsoft formats, and a lightweight footprint, making everyday document tasks easier without the need for complex coding.
- 1. Download WPS Office: Visit the official WPS website and download the free installer for your operating system.
- 2. Install the Software: Run the setup file and follow the quick on-screen instructions to complete the installation.
- 3. Open Your Documents: Launch WPS Writer to seamlessly open, edit, and manage your existing Word documents.

Frequently Asked Questions
What is the difference between a Bookmark and a Document Variable in Word?
A Bookmark marks a physical, visible location or text selection within the document's content. A Document Variable is hidden string data stored within the document's backend, which is only displayed on the page if you explicitly insert a DOCVARIABLE field.
Why do I get an error when using the Range property on a Document Variable?
Document variables store values purely as strings and do not exist as physical text ranges in the document. Because they lack physical placement, they do not support the .Range property. You must use the .Value property instead.
How do I make the updated document variable visible in my Word document?
You must insert a DOCVARIABLE field (e.g., { DOCVARIABLE bmprogramabbrv }) in your document where you want the text to appear. After updating the variable via VBA, run the ActiveDocument.Fields.Update command to refresh the fields so the new text is displayed.




