VBA & Macro Problems

How to Replace Bookmark Text with Document Variables in Word VBA

Steven ParkerSteven Parker Nov 2, 2026 869 views

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.

How to Replace Bookmark Text with Document Variables in Word VBA
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.
Before you start

Ensure your Word document contains the correct bookmarks and that you have enabled the Developer tab to access the VBA editor.

Solution 1Recommended

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.

1
Open the VBA Editor

Press Alt + F11 in Word to open the Microsoft Visual Basic for Applications editor.

2
Locate Your Procedure

Find the module or UserForm code where you are handling the document variables and bookmarks.

3
Assign the Value

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.

4
Update Document Fields

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.

Use the Value Property to Update Document Variables
Tip: You can also wrap your code in a 'With ActiveDocument' block to make it cleaner: With ActiveDocument .Variables("bmprogramabbrv").Value = .Bookmarks("bmprogramabbrv").Range.Text .Fields.Update End With
Free Microsoft Office alternative

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. 1. Download WPS Office: Visit the official WPS website and download the free installer for your operating system.
  2. 2. Install the Software: Run the setup file and follow the quick on-screen instructions to complete the installation.
  3. 3. Open Your Documents: Launch WPS Writer to seamlessly open, edit, and manage your existing Word documents.
Free, lightweight, and features a familiar UI for seamless migration from MS OfficeFully compatible with Microsoft Word formats, including DOCX and DOCMSimplifies document generation and form filling without relying solely on complex document variables
microsoft office alternative - wps office

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.