Class Variable (Word VBA)

The class Variable 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. To use a Variable class variable it first needs to be instantiated, for example


Dim vrb as Variable
Set vrb = ActiveDocument.Variables(Index:=1)

For Each

Here is an example of processing the Variable items in a collection.


Dim vrb As Variable
For Each vrb In ActiveDocument.Variables
	
Next vrb

Delete

Deletes the specified variable.


ActiveDocument.Variables(1).Delete

Index

Returns a Long that represents the ordinal position of a variable with in the collection of variables.


Set myVar = ActiveDocument.Variables.Add(Name:="Name", _ 
 Value:="Joe") 
num = myVar.Index

Name

Returns the name of the specified object.


Dim strName As String
strName = ActiveDocument.Variables(1).Name

Value

Returns or sets the value of a document variable.


ActiveDocument.Variables.Add Name:="Temp2", Value:="10" 
MsgBox ActiveDocument.Variables("Temp2").Value