A common characteristic of many Word macros is a program statement that changes the structure of a document or a command option in the word processor itself. For example, you might choose to change the line spacing in a particular paragraph to double space, or you might change Word's document view to Page Layout view. To make such a change in a Visual Basic macro, you need to use a constant in your program statement, a special value supplied by Office to adjust settings in each Office application.
True to its name, a constant is a named value that doesn't change while your macro runs. It replaces a number or word in your macro with a coded label that you can easily remember. You can create your own constants to store information, as you'll learn later in the chapter, but the most useful constants are special values called intrinsic constants that Office applications define in object libraries for your use.
For example, to change Word's document view to Page Layout, you could use the wdPageView constant, as shown in the following program statement:
ActiveWindow.View.Type = wdPageView |
This example contains the following elements:
NOTE
The letters wd at the beginning of the wdPageView constant identify it as an intrinsic constant in the Microsoft Word object library. The Word object library is a special file declaring objects, properties, methods, and constants that is automatically included in Word macros. Constants in the Word object library actually represent down-to-earth numbers; for example, wdPageView contains the number 3. But when you write Word macros, you'll find the constant names much easier to remember.Other Office applications have their own constant prefixes and object libraries, including xl (Microsoft Excel object library), ac (Microsoft Access object library), and vb (Visual Basic for Applications object library).
Take a moment now to try a simple example using Word constants. In this exercise, you'll create a macro, called CenterHeading, that uses Word constants to format selected text in a document with shading, border formatting, and center alignment. You'll also learn how to type in a new macro from scratch using the Visual Basic Editor, a technique you'll return to often in Part 8.
ON THE WEB
The CenterHeading macro is stored in the Chap59 document on the Running Office 2000 Reader's Corner page. For information about connecting to this Web site, read the Introduction.
To create the CenterHeading Macro, follow these steps:
In this exercise, you'll create the CenterHeading macro in a new document file, not in the Normal.dot template.
Word opens the Macros dialog box, the place where you create and run Visual Basic macros.
Word starts the Visual Basic Editor and opens a new macro procedure named CenterHeading in the Code window. When you create a new macro from scratch, you use the Code window to type in the program statements that make up the macro. You enter your code between the Sub and End Sub statements, which mark the beginning and ending of the macro, respectively.
When you type a period after an object name that the Visual Basic Editor recognizes, a drop-down list box appears that contains a list of the objects, properties, and methods that are compatible with it. Each of the program statements in this macro will begin with the Selection object, because you are formatting selected text in your Word document.
The Visual Basic Editor displays the expression Selection.Shading.Texture in the Code window.
Congratulations! You have completed your first program statement, a command that adds 10 percent background shading to the selected text in your Word document.
Selection.Borders(wdBorderBottom).LineStyle = wdLineStyleSingle Selection.Borders(wdBorderBottom).LineWidth = wdLineWidth150pt Selection.ParagraphFormat.Alignment = wdAlignParagraphCenter |
The first two program statements, which use the Borders property, will format the selected paragraph using a single underline border that is 1.5 points wide when you run the macro. Notice that two Word constants are used in each program statement, a constant that identifies which border is being formatted (wdBorderBottom), and a constant that selects the formatting options you have chosen (wdLineStyleSingle and wdLineWidth150pt). The final program statement uses the ParagraphFormat property to set the paragraph alignment of the selected paragraph to center alignment.
Now run the CenterHeading macro in your Word document to create the custom formatting effect. Follow these steps:
Word displays the blank document containing the CenterHeading macro.
NOTE
The CenterHeading macro is designed for formatting paragraphs only. (You need to select everything you typed, including the end-of-paragraph mark.) If you select only a few words or characters in a line, you'll get different results than the ones we show here.
Word runs your macro and formats the Table of Contents heading by using shading, border, and center alignment commands, as shown in Figure 38-3, on the next page. If you want to save this macro, return to Word and save the file containing the macro by choosing Save As from the File menu.
Note that in this part of the book you might see line continuation characters at the end of some Visual Basic code lines. The line continuation character (_) is simply a device we are using to indicate line breaks for lines that are longer than 60 characters (for better readability). These breaks are acceptable to the Visual Basic interpreter. If you choose, you can type each of these long statements on one line (as shown in the following illustration) if you don't include the continuation character. However, you might find the line continuation character useful if you want to see all your code at once. (The Code window can actually scroll to the right up to 1024 characters.)
Figure 38-3. The CenterHeading macro demonstrates the formatting power of Word constants.
WARNING
You cannot use a line continuation character to break a string that is enclosed in quotation marks.