What is a DOCVARIABLE
in Microsoft Word 2003? How do I set it? How do I make it display in my Word document?
Asked
Active
Viewed 4.0k times
15

Gottlieb Notschnabel
- 9,408
- 18
- 74
- 116

joe
- 16,988
- 36
- 94
- 131
3 Answers
7
You can use the Microsoft Visual Basic for Applications Variables collection to set and retrieve the contents of a string variable in a Word document or template.
Also, you can use the DocVariable field to retrieve the value of a document variable after it has been set to display within a Word document.
Source: How to store and retrieve variables in Word documents
Sub GetSetDocVars()
Dim fName As String
fName = "Jeff Smith"
' Set contents of variable "fName" in a document using a document
' variable called "FullName".
ActiveDocument.Variables.Add Name:="FullName", Value:=fName
' Retrieve the contents of the document variable.
MsgBox ActiveDocument.Variables("FullName").Value
End Sub

Community
- 1
- 1

Chris Serra
- 13,226
- 3
- 25
- 25
-
But like where does this code goes into? is it a Word script or something? – Christopher Francisco Dec 16 '16 at 14:28
-
This method will cause errors to be thrown if the method is run more than once. VBA (tested in Word 2013) will allow you to create/set values on the fly using for example `ActiveDocument.Variables("FieldName").Value = fieldValue` – Shawn Dec 16 '19 at 21:29
4
How do I make it display in my word document:
Insert->Field->Category:DocumentAutomation->Field Names:DocVariable->Field COdes Button-> Then enter the name of the variable.

joe
- 16,988
- 36
- 94
- 131
0
I was also looking for an answer to this question. Created a small script to display all ActiveDocument.Variables
Here is it:
Sub GetVariables()
' Declaration of output variavle, which is a string
Dim output As String
output = ""
For Each Variable In ActiveDocument.Variables
' & is used for string concatenation.
output = output & Variable.Name & " = " & Variable & vbNewLine
Next
MsgBox output
End Sub

wojtekc
- 61
- 1
- 2