How to store and retrieve variables in Word documents

Article translations Article translations
Article ID: 306281 - View products that this article applies to.
This article was previously published under Q306281
For a Microsoft Word 2000 version of this article, see 209227.
For a Microsoft Word 97 version of this article, see 181816.
For a Microsoft Word 7.0 version of this article, see 106388.
Expand all | Collapse all

On This Page

SUMMARY

This article describes how to store and retrieve document variables while you are using Microsoft Word.

MORE INFORMATION

Microsoft provides programming examples for illustration only, without warranty either expressed or implied. This includes, but is not limited to, the implied warranties of merchantability or fitness for a particular purpose. This article assumes that you are familiar with the programming language that is being demonstrated and with the tools that are used to create and to debug procedures. Microsoft support engineers can help explain the functionality of a particular procedure, but they will not modify these examples to provide added functionality or construct procedures to meet your specific requirements.
For more information about how to use the sample code in this article, click the article number below to view the article in the Microsoft Knowledge Base:
290140 OFFXP: How to Run Sample Code from Knowledge Base Articles
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.

How to Set and Retrieve the Value of a Document Variable

The following sample macro uses the Variables property to store and then retrieve the value of a string variable named "FullName" with the current document.
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
				
NOTE: The following error message appears if the Variable name is already set in the document:
Run-Time Error "5903": The Variable name already exists.
You must delete the name or just reset the value argument.

How to Delete a Document Variable

The following sample macro uses the Variables property to first set and retrieve the value of a document variable and then delete the variable from the active document.
Sub GetSetDeleteDocVars()

   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
   ' Delete the variable.
   ActiveDocument.Variables("FullName").Delete

End Sub
				

How to Retrieve the Value of a Document Variable by Using the DocVariable Field

The following sample macro uses the Variables property to set a document variable. Following the sample macro are steps to use the DocVariable field to retrieve the value into the contents of the same document.
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

End Sub
				
To use the DocVariable field, follow these steps:
  1. On the Insert menu, click Field.

    Note In Microsoft Office Word 2007, click Quick Parts in the Text group on the Insert tab, and then click Field.
  2. In the Categories box, select Document Automation.
  3. In the Field names list, select DocVariable.
  4. In the New Name box, under Field properties, type the name of the document variable.

    NOTE: If you see Advanced field properties instead of Field properties, click Hide Codes.
  5. Click OK.
If you need to preserve a value between sessions of a macro, you can store values by using any of the following methods:
  • Use the PrivateProfileString property to store values to a private settings file.

    For more information about the PrivateProfileString property, in the Visual Basic Editor, click Microsoft Visual Basic Help on the Help menu, type PrivateProfileString Property in the Office Assistant or the Answer Wizard, and then click Search to view the topic.
  • Use the Variables property to store values in a document.

    For more information about the Variables property, in the Visual Basic Editor, click Microsoft Visual Basic Help on the Help menu, type Variables property in the Office Assistant or the Answer Wizard, and then click Search to view the topic.
  • Use the Visual Basic for Applications Input/Output statements to write to a text file (for example, a Write statement or Print statement).

    For more information about writing data to files, in the Visual Basic Editor, click Microsoft Visual Basic Help on the Help menu, type writing data to files in the Office Assistant or the Answer Wizard, and then click Search to view the topic.
  • Use the AutoTextEntry object to store values in an AutoText entry. For more information about the AutoTextEntry object, in the Visual Basic Editor, click Microsoft Visual Basic Help on the Help menu, type AutoTextEntry object in the Office Assistant or the Answer Wizard, and then click Search to view the topic.

Properties

Article ID: 306281 - Last Review: January 31, 2007 - Revision: 4.1
APPLIES TO
  • Microsoft Office Word 2007
  • Microsoft Office Word 2003
  • Microsoft Word 2002 Standard Edition
Keywords: 
kbdtacode kbhowto kbprogramming KB306281

Give Feedback

 

Contact us for more help

Contact us for more help
Connect with Answer Desk for expert help.
Get more support from smallbusiness.support.microsoft.com