Fix Document Variable Errors in a Word VBA Template
Question details
The user needs to prevent document variable errors when a Word template updates fields before a user form supplies their values.

- Product
- Microsoft Word
- Device & OS
- not provided
- Scenario
- A VBA template creates a document containing DOCVARIABLE fields whose values are supplied by a user form.
- Observed behavior
- Word displays “Error! No document variable supplied” when fields are updated before the associated variables have values.
Press Alt+F9 in Word to inspect the DOCVARIABLE field codes and identify their variable names. Open the macro-enabled template in Word and enable macros only if you trust its source.
Initialize Word document variables before updating fields
Use this approach when a new document updates DOCVARIABLE fields before the user form has assigned their values.
Initialize every variable referenced by a DOCVARIABLE field. A single space can serve as an initial value when the field should appear blank.
Use one appropriate creation event, such as Document_New or AutoNew, rather than relying on an assumed order between events.
In the template, press Alt+F9 and inspect each DOCVARIABLE field, such as { DOCVARIABLE CustomerName }. Confirm that each field includes the intended variable name.
Press Alt+F11, find the template in Project Explorer, and open its ThisDocument module. Save the template as a macro-enabled Word template (.dotm) if needed.
In the template's ThisDocument module, add a Document_New procedure that initializes each required variable in the new document. For example, use the following for CustomerName; repeat the call for the other field names: Private Sub Document_New() InitializeVariable ActiveDocument, "CustomerName" End Sub Private Sub InitializeVariable(ByVal doc As Document, ByVal variableName As String) Dim v As Variable For Each v In doc.Variables If StrComp(v.Name, variableName, vbTextCompare) = 0 Then v.Value = " " Exit Sub End If Next v doc.Variables.Add Name:=variableName, Value:=" " End Sub
In the user form's completion button code, assign each form value to ActiveDocument.Variables("CustomerName").Value, substituting a single space for an empty value. After all assignments, run ActiveDocument.Fields.Update to refresh fields in the main document.
Create a new document from the template, complete the form, and check the field results. If a field is in a header or footer, select it there and press F9 to test its update separately.

Edit Word Documents with WPS Office
WPS Office is a free, lightweight alternative with a familiar interface for opening and editing Word documents. Its format compatibility helps you migrate everyday documents, but it does not fix Microsoft Word VBA template errors.

Frequently Asked Questions
Why does my DOCVARIABLE field show an error?
The field may be updated before its document variable has a value, or its field code may not contain the intended variable name. Press Alt+F9 to inspect the code, then initialize the named variable before updating the field.
How can I make a document variable appear blank?
Assign the variable a single space, such as ActiveDocument.Variables("CustomerName").Value = " ", before updating the corresponding field.
Where should I put the initialization code?
In the template's VBA project, open ThisDocument and use its Document_New event, or use an appropriate AutoNew macro. Initialize the variables before any code updates the fields.
Does ActiveDocument.Fields.Update refresh fields in headers and footers?
ActiveDocument.Fields.Update refreshes fields in the main document story. To test a field in a header or footer, select the field there and press F9; automated updates for those areas require handling their story ranges separately.
Can WPS Office repair the Word VBA template?
WPS Office can be used as an alternative for everyday document editing, but it does not repair Microsoft Word VBA template errors. Make and test the VBA changes in Word.




