|
|
Working in the VB Editor to create code or a form is not much different from creating a Project custom form in the Microsoft Form Editor (discussed in Chapter 25) except that the VB Editor is more powerful and easier to use than the Form Editor! As you’ll see, the “visual” part of Visual Basic comes from the fact that most of the code is generated by drawing controls onto a virtual canvas—a custom form.
To open the VB Editor from within Project, follow these steps:
Select Tools Ø Macro Ø Visual Basic Editor, or press Alt+F11. This opens the VB Editor workspace, as shown in Figure 26.1. If this is the first time you’ve opened the VB Editor, the workspace is a blank sheet; otherwise, there are two windows open, as described next.
Figure 26.1: The VB Editor
If the Project Explorer window, shown in Figure 26.2, is not yet open, select View Ø Project Explorer.
This window lists all open VBA projects. Every document can have one project associated with it; the project is stored with the document. There are three types of project components: project objects, forms, and modules, which are containers for VB code. Projects can include two other types of project components: classes, and references to templates or other documents.
Figure 26.2: The Project Explorer
If the Properties window shown in Figure 26.3 is not yet open, select View Ø Properties Window.
This window contains the properties for the object selected in the Project Explorer. If you have worked with Microsoft Access, you already have experience with Properties windows. If not, browse through the properties for your currently active project, and you will see that display, prioritization, and function defaults are defined in this window. Every object you create has its own unique set of properties; you select the value for each property in the Properties window. This window also displays any changes you might have made to your project defaults.
Figure 26.3: The Properties window
Note | The Project Explorer and Properties windows may open automatically when you launch the VB Editor. |
One important window that you haven’t seen yet is the Code window, which displays Visual Basic code from the active component in the Project Explorer. To open the Code window, shown in Figure 26.4, double-click an item in Project Explorer, or select the item and then click the View Code button.
Note | Code lines that begin with an apostrophe (') are comments that explain the code. In the VB Editor, comments are color-coded green. The code itself is displayed in the default text color, black. |
Figure 26.4: The Code window
Microsoft Project macros are stored in Visual Basic modules, so all the macros you’ve already created with the Macro Recorder (in Chapter 25) are actually written in VB, and available for editing in the VB Editor.
To examine or edit a macro, choose Tools Ø Macros to open the Macros dialog box, select the macro you want to examine, and click the Edit button. This opens the VB Editor with the selected macro open for editing. You can also go directly to the VB Editor by choosing Tools Ø Macro Ø Visual Basic Editor.
Each macro you create with the Macro Recorder is placed in a new module with its own code window; if you have a lot of macros, the Project Explorer will quickly fill up with modules. To make these modules easier to manage, you can use cut and paste to move the macros to a single code window, or use two or three modules to organize your macros by functionality (and then delete the empty modules).
To delete a module, right-click it in the Project Explorer and then choose Remove Module n from the shortcut menu, where n is the name of the macro. In addition, you can use the Organizer to work with modules and macros, and decide which should reside only in your own project and which should be copied to global.mpt. (See Chapter 23 for information about how to use the Organizer.)
|
|