Another useful application for variables is holding portions of your document while your macro runs. For example, you might use a variable to hold a copy of a paragraph temporarily while you rearrange the paragraph's contents or move it to a new location. However, because documents and their contents are represented by objects in Visual Basic, you'll need to create a special container called an object variable when you want to reference an object in Office.
To declare an object variable in Visual Basic, use the following syntax:
Dim ObjectVar As Object
In the Dim statement, ObjectVar is the name of the variable you'll assign the object to later in your program code. For example, to create an object variable to hold text, you might use the following Dim statement:
Dim myText as Object
After you dimension an object variable, you can use it to reference an Office object by creating a Set statement, following this syntax:
Set ObjectVar = ObjectName
In the Set statement, ObjectVar is the name of your object variable, and ObjectName is an expression that returns an Office object. For example, to assign a Word Range object containing the text from the first paragraph in the active document to the myText object variable, you might use the following Set statement:
Set myText = ActiveDocument.Paragraphs(1).Range
After you assign an object to the object variable, you can use the variable just as you would the object. Thus, object variables save you typing time because the object variable names are usually shorter than the full object names. We'll use this method to work with Office objects regularly in Part 8 of this book.
ON THE WEB
The CopyParagraph macro is located in the Chap59 document on the Running Office 2000 Reader's Corner page.
One practical use for an object variable is to hold a range reference when you copy text from one location to another. Follow these steps to create a macro that copies the first paragraph of the active document to a new document:
Word starts the Visual Basic Editor and opens a new macro procedure named CopyParagraph in the Code window.
Type the following program statements:
Dim myText As Object Set myText = ActiveDocument.Paragraphs(1).Range Documents.Add Selection.InsertAfter myText
The first statement declares a variable, myText, of type Object to hold the reference to a Range object. The second statement then assigns a Range object representing the first paragraph in the active document to myText. The object expression contains a collection index (1), which specifies the first paragraph in the Paragraphs collection. (The second paragraph has an index of 2, the third paragraph an index of 3, and so on.)
The Range object contains the text of the first paragraph only, not the formatting. If you also want to copy the formatting of the paragraph, create a second object variable and use the Duplicate property of the Range object to copy the formatting.
Next the Add method adds a new document to the Documents collection, and the InsertAfter method inserts the current value of the myText object variable into the new Word document.
Now run the CopyParagraph macro in your Word document. Follow these steps:
Click the View Microsoft Word button on the Visual Basic Editor toolbar.
Word displays the active document. If you have been following the exercises so far in this chapter, your document will contain two headings: Table of Contents and Visual Basic Is Fun! The CopyParagraph macro should copy the first heading, Table of Contents, to a new document.
Choose Macros from the Macro submenu of the Tools menu.
Word runs your macro and creates a new document containing the Table of Contents paragraph (the text only, not the formatting).