Using Object Variables to Process Text

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.

The CopyParagraph macro is located in the Chap59 document on the Running Office 2000 Reader's Corner page.

Using an Object Variable to Copy Text

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:

  1. In Word, choose Macros from the Macro submenu of the Tools menu.
  2. Specify the document in which you want to store the macro (Chap59.doc or other) using the Macros In list box.
  3. Type CopyParagraph in the Name text box, and then click the Create button.

    Word starts the Visual Basic Editor and opens a new macro procedure named CopyParagraph in the Code window.

  4. 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.)

  5. NOTE
    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:

  1. 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.

  2. Choose Macros from the Macro submenu of the Tools menu.

  3. Select the CopyParagraph macro, and then click Run.

    Word runs your macro and creates a new document containing the Table of Contents paragraph (the text only, not the formatting).

  4. click to view at full size.

  5. After you have verified the operation of your macro, close the new document and discard your changes. (You won't need to save the new document in this chapter.)
  6. Display the Word document containing your new macro, and then click the Save button to save the CopyParagraph macro to disk.

Running Microsoft Office 2000 Small Business
Running Microsoft Office 2000
ISBN: 1572319585
EAN: 2147483647
Year: 2005
Pages: 228 © 2008-2017.
If you may any questions please contact us: