You open the Visual Basic Editor from the host application that you're using. For example, if you're working in Word, you open the Visual Basic Editor from Word. The instance of the Visual Basic Editor that you open is then associated with Word.
However, you can open two or more instances of the Visual Basic Editor. For example, if you've already opened an instance of the Visual Basic Editor in Word, you could open another instance of the Visual Basic Editor in Excel, and then another in Access.
You can open the Visual Basic Editor in two ways:
Specify the macro that you want to edit. The host application then opens the Visual Basic Editor and displays the macro, so that you're ready to work with it.
Open the editor directly, and then locate the macro code you want to work with.
The next two sections demonstrate the two ways of opening the Visual Basic Editor, and the third section shows you how to navigate to a macro.
If you know which macro you want to work with, use this method to open the Visual Basic Editor and the macro at the same time. This example uses Word to open the Transpose_Word_Right macro that you recorded in Chapter 1.
Open Word if it's not already running.
Press Alt+F8 to display the Macros dialog box.
Select the Transpose_Word_Right macro and click the Edit button. Word opens the Visual Basic Editor with the macro displayed and ready for editing, as shown in Figure 2.1.
Choose File Close and Return to Microsoft Word to close the Visual Basic Editor for the moment, so that you can open it using the method described in the next section.
To open the Visual Basic Editor directly, follow these steps:
Open or activate the host application. In this case, open or switch to Word.
Press Alt+F11. The Visual Basic Editor opens.
After opening the Visual Basic Editor directly, use the Project Explorer pane (shown on the left side in Figure 2.1 ) to navigate to your macro. You also use the Project Explorer to navigate among open projects and modules when you're working in the Visual Basic Editor.
The Project Explorer Resembles Windows Explorer Folder ViewThe Project Explorer pane works like a standard Windows Explorer tree when you're viewing folders and subfolders. Depending on the application you're using, you'll see different projects displayed in the tree (more on this later in the chapter). |
To navigate to the Transpose_Word_Right macro, follow these steps:
In the Project Explorer pane in the upper-left corner of the Visual Basic Editor, expand the entry for Normal (which represents Normal.dotm, the Normal template) by clicking the+sign to the left of its name. (If the Normal entry is expanded, skip this step.)
Double-click the Modules entry to expand it.
Double-click the NewMacros module. (This is the module in which Word automatically creates the macros you record.) The Visual Basic Editor displays the contents of the module in the Code window on the right side.
If the module contains more than one macro, you'll also need to select the macro you want to work with — in this case, the Transpose_Word_Right macro. (If you've recorded only the Transpose_Word_Right macro, only this macro appears in the Code window.) To select a macro, use one of these methods:
In the Code window, select the macro from the Procedure dropdown list, as shown in Figure 2.2. (If you hover the mouse pointer over the list before dropping it down, you'll see a ScreenTip that gives its name: Procedure.)
Use the scroll bar to scroll to the macro you want to edit, which is identified by the word Sub, the name you gave it, and a pair of parentheses — in this case, Sub Transpose_Word_Right().
3.143.239.50