VBA & Macro Problems

Fix Word VBA UserForm Values Not Updating Document Variables

Aamir Naveed AkramAamir Naveed Akram Nov 18, 2026 870 views

Question details

Transfer values entered in a Word VBA UserForm to document variables and display them in DOCVARIABLE fields.

Fix Word VBA UserForm Values Not Updating Document Variables
Product
Microsoft Word VBA
Device & OS
not provided
Scenario
A UserForm collects information that should populate fields in a Word document.
Observed behavior
The entered values are not transferred to document variables or displayed in DOCVARIABLE fields.
Before you start

Open the document in desktop Word and save it as a macro-enabled document or template, such as DOCM or DOTM. Check the names of the UserForm controls and DOCVARIABLE fields, and enable macros only if you trust the file.

Solution 1Recommended

Assign UserForm values and update DOCVARIABLE fields

Use the form button's click procedure to save each value to a matching document variable before refreshing the fields.

1
Insert a DOCVARIABLE field

In the document body, press Ctrl+F9 to insert field braces, type DOCVARIABLE CompanyName between them, and press F9. Use the exact variable name you will reference in VBA; do not type the braces manually.

2
Open the form's button procedure

Press Alt+F11 to open the VBA editor, locate the UserForm in Project Explorer, and double-click its submit button to open the Click procedure.

3
Assign the value and refresh fields

Replace the button procedure with the following example, changing cmdSubmit and txtCompanyName to your actual control names: Private Sub cmdSubmit_Click() Dim doc As Document Dim v As Variable Dim companyName As String companyName = Me.txtCompanyName.Value If Trim$(companyName) = "" Then MsgBox "Enter a company name." Me.txtCompanyName.SetFocus Exit Sub End If Set doc = ActiveDocument On Error Resume Next Set v = doc.Variables("CompanyName") On Error GoTo 0 If v Is Nothing Then doc.Variables.Add Name:="CompanyName", Value:=companyName Else v.Value = companyName End If doc.Fields.Update Me.Hide End Sub

4
Add and test other values

For each additional text box, validate its value and repeat the variable lookup and assignment using its matching DOCVARIABLE name. Save the file, open the UserForm, enter values, and click the submit button.

Assign UserForm values and update DOCVARIABLE fields
Fields outside the document body: ActiveDocument.Fields.Update updates fields in the main document story. Fields in headers, footers, or other stories may need to be updated separately.
Free Microsoft Office alternative

Edit Word documents with WPS Writer

WPS Office is a free, lightweight alternative with a familiar interface for working with Word documents and moving everyday editing tasks to WPS Writer. It does not fix Microsoft Word VBA code, so test any macro-dependent documents in Word.

Open and edit Microsoft Word DOCX and DOC filesUse a familiar document-editing interface for everyday workWork with documents in a lightweight office suiteMove non-macro editing tasks to WPS Writer
microsoft office alternative - wps office

Frequently Asked Questions

Why does the form close without updating the document?

The button's Click procedure may validate the inputs and hide the form without assigning the values. In the VBA editor, add the document variable assignments and a field update before Me.Hide or Unload Me.

How do I insert a DOCVARIABLE field in Word?

Click where the value should appear, press Ctrl+F9, and type DOCVARIABLE followed by the variable name inside the field braces. Press F9 to update the field.

What if the document variable does not exist yet?

Create it with ActiveDocument.Variables.Add, providing its name and value. If it already exists, assign the new value to its Value property instead.

Why is a field in the header still showing an old value?

ActiveDocument.Fields.Update updates the main document story, not necessarily headers or footers. Open the header or footer, select the field, and press F9, or update those stories separately in VBA.