Variable Object
Word Developer Reference |
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.
Remarks
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.
Visual Basic for Applications |
---|
|
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.
Visual Basic for Applications |
---|
|
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.
Visual Basic for Applications |
---|
|
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.
Visual Basic for Applications |
---|
|
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.
Visual Basic for Applications |
---|
|
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.
Visual Basic for Applications |
---|
|
See Also