Class Variables (Word VBA)

A collection of Variable objects that represent the variables added to a document or template. Document variables are used to preserve macro settings in between macro sessions. To use a Variables class variable it first needs to be instantiated, for example


Dim vrbs as Variables
Set vrbs = ActiveDocument.Variables

For Each

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


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

Add

Returns a Variable object that represents a variable added to a document.

Document variables are invisible to the user unless a DOCVARIABLE field is inserted with the appropriate variable name. If you try to add a 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 a new variable to it.

Add (Name, Value)


With ActiveDocument 
 .Variables.Add Name:="Temp", Value:="12" 
 .Fields.Add Range:=Selection.Range, _ 
 Type:=wdFieldDocVariable, Text:="Temp" 
End With 
ActiveDocument.ActiveWindow.View.ShowFieldCodes = False

Arguments

The following argument is required

Name (String) - The name of the document variable.

Optional arguments

The following argument is optional

Value (Variant) - The value for the document variable.

Count

Returns a Long that represents the number of variables in the collection.


Dim lngCount As Long
lngCount = ActiveDocument.Variables.Count

Item

Returns an individual Variable object in a collection.

Item (Index)

Index: The individual object to be returned. Can be a Long indicating the ordinal position or a String representing the name of the individual object.


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