2.1. Opening The Visual Basic Editor

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.

2.1.1. Opening the Visual Basic Editor with a Macro Selected

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.

  1. Open Word if it's not already running.

  2. Press Alt+F8 to display the Macros dialog box.

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

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

Figure 2.1. The Visual Basic Editor with the Transpose_Word_Right macro open in the Code window

2.1.2. Opening the Visual Basic Editor Directly

To open the Visual Basic Editor directly, follow these steps:

  1. Open or activate the host application. In this case, open or switch to Word.

  2. Press Alt+F11. The Visual Basic Editor opens.

The Visual Basic Editor Remembers Its Code Window

Depending on the state of the Visual Basic Editor the last time it was closed, you may see one or more Code windows open. For example, if you left the Code window for the NewMacros module open in the previous section, the Visual Basic Editor will display this Code window again.


2.1.3. Navigating to a Macro

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 View

The 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:

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

  2. Double-click the Modules entry to expand it.

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

Figure 2.2. If the module contains two or more macros, scroll to the macro you want to edit, or select it from this Procedure dropdown list.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
3.143.239.50