Fix Word VBA UserForm Values Not Updating Document Variables
Question details
Transfer values entered in a Word VBA UserForm to document variables and display them in DOCVARIABLE fields.

- 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.
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.
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.
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.
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.
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
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.

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.

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.




