Using Visual Basic to Edit a Macro

In Office 2000, you can edit your recorded macros and create new macros from scratch by using the Visual Basic development environment, a special utility that has its own windows, menus, and programming tools. (Word, Excel, PowerPoint, and Access all include this program, although in Access you can't use it to edit recorded macros.) In this section, you'll learn how to use the Visual Basic development environment (also called the Visual Basic Editor) to edit the InsertAddress macro. Feel free to experiment a little in this section— the skills you learn will come in handy each time you work with macros.

To edit your InsertAddress macro using Visual Basic, follow these steps:

  1. On the Tools menu, point to Macro, and then click Macros on the submenu. You'll see the Macros dialog box, which lists the InsertAddress macro in the Macro Name list box.
  2. Click the InsertAddress macro, and then click the Edit button in the Macros dialog box. The Visual Basic development environment starts, as shown in Figure 37-1.

click to view at full size.

Figure 37-1. The Visual Basic development environment allows you to create new macros and modify existing ones.

The Visual Basic development environment (Visual Basic, for short) includes a variety of programming tools to help you write, edit, test, and manage your Word macros. The most important tools to identify now are the Project Explorer window, the Properties window, and the Code window. (We'll discuss each in this chapter.) You might see these tools in a slightly different arrangement on your screen if someone else has used your computer or if your setup options were different than ours.

The Code window is a large text editing window that displays the contents of your Office macro in a programming language called Visual Basic. Each program statement in this macro follows a particular programming rule, and the trick to learning macro programming in Office is understanding the syntax principals and program logic behind each of the Visual Basic statements.

TIP
Another name for the exact spelling, order, and spacing of keywords in a macro is statement syntax.

The macro is stored in a special program code container called a subroutine, which is part of the more comprehensive container called a module. This particular subroutine is called InsertAddress, and it is delimited (or enclosed as a block of macro text) by the Sub and End Sub statements, respectively. Within the body of the subroutine, you'll see descriptive comments, which appear in green type, and the Visual Basic program statements that do the work of the macro, which appear in black type. In this macro, the Sub and End Sub statements appear in blue type, because they are special reserved words in the Visual Basic programming language called keywords.

Follow these steps to add a descriptive comment to the macro and to edit the business phone number.

  1. Press the Down arrow key three times, and then press the Right arrow key once to move the insertion point after the fourth single quotation mark in the macro. (You can also use the mouse to move the insertion point if you prefer.)
  2. Press Spacebar once, type My first macro, and press Enter.

    Visual Basic inserts your new comment and displays it in green type. Comments are for documentation purposes only, and are not used by the macro when it runs. We recommend that you use comments as informal notes about how your macro works if you plan to share your macro with friends and work associates. (Comments are especially useful when you write complicated program statements.) Your Code window should look like this:

    click to view at full size.

  3. Now move the insertion point to the program statement containing the phone number you entered, and change the last four digits to 1234. Your Code window should look like this:
  4. click to view at full size.

    This simple edit will change the content of your macro each time you run it. Although we're not focusing on the exact details of the Visual Basic program statements right now, you might find it interesting to know what the words Selection and TypeText are doing in the program code. Selection is an object, or a component of the current document or application that can be programmatically controlled. The Selection object designates a particular location in your document for action— either the text block that is highlighted, or the current insertion point.

    TypeText is a method, or a command that can be executed using the specified object. When the TypeText method is used with the Selection object, Word inserts the text specified at the current insertion point— exactly what your InsertAddress macro does when you run it!

    You're finished editing the InsertAddress macro for now. In the next section, you'll run the macro again and save your changes to disk.



Running Microsoft Office 2000 Small Business
Running Microsoft Office 2000
ISBN: 1572319585
EAN: 2147483647
Year: 2005
Pages: 228

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