For a Microsoft Word 2000 version of this article, see209227. For a Microsoft Word 97 version of this article, see181816. For a Microsoft Word 7.0 version of this article, see106388.

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").ValueEnd 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").DeleteEnd 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:=fNameEnd 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 ofField 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.

Need more help?

Want more options?

Explore subscription benefits, browse training courses, learn how to secure your device, and more.

Communities help you ask and answer questions, give feedback, and hear from experts with rich knowledge.