Using Office Constants

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:

  • The ActiveWindow object, which represents the current, open window in Word
  • The View property, which returns an object representing the active view in the active window
  • The Type property, which sets the document view type for the window (Options include Normal, Online Layout, Page Layout, Master, or Outline.)
  • The wdPageView Word constant, which sets the view to Page Layout view (Other useful constants include wdNormalView, wdOnlineView, wdMasterView, and wdOutlineView.)

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

Using Constants to Create Custom Formatting

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:

  1. Start Word and open a new, blank document.

    In this exercise, you'll create the CenterHeading macro in a new document file, not in the Normal.dot template.

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

    Word opens the Macros dialog box, the place where you create and run Visual Basic macros.

  3. Type CenterHeading in the Name text box, and then click the Macros In drop-down list box and select your new, blank document in the list. (In this example, the new document is named Document1.)
  4. Click the Create button.

    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.

  5. Begin your macro now by typing the object name Selection followed by a period.

    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.

    click to view at full size.

  6. Scroll down the drop-down list box, and double-click the Shading property. Then type a period to display a second drop-down list box.

    click to view at full size.

  7. Double-click the Texture property in the list box to build a program statement that adjusts the shading formatting of the selected text in your document.

    The Visual Basic Editor displays the expression Selection.Shading.Texture in the Code window.

  8. Now finish your program statement by typing an equal sign (=), double-clicking the wdTexture10Percent constant in the drop-down list box, and pressing Enter.

    Congratulations! You have completed your first program statement, a command that adds 10 percent background shading to the selected text in your Word document.

  9. Now complete your macro by entering the following three program statements. You can either use the drop-down list boxes to pick objects, properties, and constants (as you did above), or you can type the program statements directly into the Code window.
  10.  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.

click to view at full size.

Now run the CenterHeading macro in your Word document to create the custom formatting effect. Follow these steps:

  1. Click the View Microsoft Word button on the Visual Basic Editor toolbar.

    Word displays the blank document containing the CenterHeading macro.

  2. Type Table of Contents, and press Enter to create some text that you can use to test your macro.
  3. Select the entire line or paragraph that you typed, and change the point size to 16 points. (Your macro will function perfectly at any point size, but a medium-sized font looks best for a heading.)
  4. 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.

  • Choose Macros from the Macro submenu of the Tools menu.
  • Click the CenterHeading macro if it's not already selected, and then click Run.

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

    click to view at full size.

    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.


    Running Microsoft Office 2000 Small Business
    Running Microsoft Office 2000
    ISBN: 1572319585
    EAN: 2147483647
    Year: 2005
    Pages: 228
    Authors: Michael Halvorson, Michael J. Young
    BUY ON AMAZON

    flylib.com © 2008-2017.
    If you may any questions please contact us: flylib@qtcs.net