Variable Object (Word)
Represents a variable stored as part of a document. Document variables are used to preserve macro settings in between macro sessions. The Variable object is a member of the Variables collection. The Variables collection includes all the document variables in a document or template.
Use Variables(Index), where Index is the document variable name or the index number, to return a single Variable object. The following example displays the value of the Temp document variable in the active document.
The index number represents the position of the document variable in the Variables collection. The last variable added to the Variables collection is index number 1; the second-to-last variable added to the collection is index number 2, and so on. The following example displays the name of the first document variable in the active document.
Use the Add method of the Variables collection to add a variable to a document. The following example adds a document variable named "Temp" with a value of 12 to the active document.
If you try to add a document variable with a name that already exists in the Variables collection, an error occurs. To avoid this error, you can enumerate the collection before adding any new variables. If the Blue document variable already exists in the active document, the following example sets its value to 6. If this variable does not already exist, this example adds it to the document and sets it to 6.
For Each aVar In ActiveDocument.Variables If aVar.Name = "Blue" Then num = aVar.Index Next aVar If num = 0 Then ActiveDocument.Variables.Add Name:="Blue", Value:=6 Else ActiveDocument.Variables(num).Value = 6 End If
Document variables are invisible to the user unless a DOCVARIABLE field is inserted with the appropriate variable name. The following example adds a document variable named "Temp" to the active document and then inserts a DOCVARIABLE field to display the value in the variable.
With ActiveDocument .Variables.Add Name:="Temp", Value:="12" .Fields.Add Range:=Selection.Range, _ Type:=wdFieldDocVariable, Text:="Temp" End With ActiveDocument.ActiveWindow.View.ShowFieldCodes = False
To add a document variable to a template, open the template as a document by using the OpenAsDocument method. The following example stores the user name (from the Options dialog box) in the template attached to the active document.