The Visual Basic development environment contains a number of useful programming tools to help you construct and manage your macros in Word, Excel, PowerPoint, and Access. In this section, you'll learn how to use the most important programming tools and how to organize them using a technique called docking. As you work through the remaining chapters of this part, you'll gain additional experience with the tools by constructing practical, working macros.
NOTE
If you're not interested in all the details about programming tools right now, just skim this section and move on to the next chapter. You can use this information later as reference material.
The essential Visual Basic tools include:
The menu bar in the Visual Basic development environment contains commands that are specifically designed to edit and manage your macros. These commands are identical in Word, Excel, PowerPoint, and Access. Table 37-1 describes the functionality of the menus.
Table 37-1. Menus in the Visual Basic Development Environment
Menu Name | Purpose |
---|---|
File | Saves macros, imports and exports useful routines, removes macros, and prints the contents of the Code window. |
Edit | Edits and searches for text in the Code window, formats code, and displays information about available properties, methods, and constants. |
View | Displays the various tools in the development environment. |
Insert | Extends your macro by adding new objects and features. Specifies a new code procedure, a custom dialog box (UserForm), a module, or a supporting file. |
Format | Formats the objects and text in a custom UserForm dialog box. |
Debug | Detects and fixes bugs (defects) in your macros. |
Run | Executes your macros. A special command, Break, is useful for debugging. |
Tools | Customizes your macro by providing references to other object libraries and controls, text formatting options in the Code window, and property settings. The Macros command is identical to the Macros command on your Office application's Tools menu. |
Add-Ins | Runs add-in commands and uses the Add-in Manager dialog box. |
Window | Adjusts the size and orientation of windows in the development environment and switches between open documents. |
Help | Displays online Help and connects to frequently used Visual Basic Web sites. |
Follow these steps to run the InsertAddress macro using the Run Sub/UserForm command:
Because the InsertAddress macro has no visible user interface, you won't see it run in the Visual Basic development environment. (You'll need to restore the document using the Windows taskbar to see the results.)
Run the macro again if you like, and take some time to experiment with other menus and commands.
The Visual Basic toolbars provide rapid access to the most common commands and procedures in the Visual Basic development environment. By default, only the Standard toolbar appears, but you can add special purpose Visual Basic toolbars by using the Toolbars submenu on the View menu. Figure 37-2 shows the purpose of the buttons and controls on the Standard Visual Basic toolbar.
Figure 37-2. The Standard toolbar provides the fastest way to issue commands in the Visual Basic development environment.
After you modify a macro in the Visual Basic development environment, it makes good sense to save your changes to prevent accidental data loss because of a power failure or system crash. Follow this step to save your macro:
Visual Basic saves your macro to disk. If you have been following the instructions in this chapter, this action saves the InsertAddress macro to the Normal.dot template.
TIP
The Save button on the Standard toolbar saves changes to the component that's currently highlighted in Project Explorer. To see the name of the file that Visual Basic will use when it saves, hold the mouse pointer over the Save button until the ScreenTip appears, identifying the current filename.
The Visual Basic Project Explorer is an organizational tool that displays a hierarchical list of the documents, or projects, currently open in your Office application, and any of their supporting components. Using Project Explorer, you can add or delete components from a project, compare elements and reorganize them, and display items of interest.
If the Project Explorer window isn't visible, you can display it by choosing the Project Explorer command from the View menu, or by clicking the Project Explorer toolbar icon. See Figure 37-3.
When you first start using Project Explorer, you might find its assortment of folders and components a bit confusing. Stick with it. Project Explorer is actually a useful programming tool, and its secrets can be readily comprehended. Each project name corresponds to a document or template that's currently open in your application. In the following illustration of Project Explorer, the Normal.dot template and the Document1.doc document files are shown with their supporting component files. (If you've been following the examples to this point, these are the files you'll see.) You'll see additional projects in your Project Explorer if you have other documents open, or if Microsoft Outlook is running and Word is your e-mail editor.
Figure 37-3. The Project Explorer window allows you to manage the components of a Visual Basic macro.
When you use Visual Basic, you can customize several different components of an Office document or template. As we demonstrated earlier in the chapter, you can record a macro and store it in a module that becomes part of the document or template. However, you can also add one or more of the following items to your project:
Project Explorer keeps track of all these different components and provides access to them via two special buttons: View Code and View Object. To switch from one component to the next, you click the project you want to work with, click the folder you want to open, and then click the component you want to view. If the component contains program code (such as a module or a UserForm), you can examine it by clicking the View Code button. If the component contains a user interface (such as a Word document or a UserForm), you can examine it by clicking the View Object button.
Follow these steps to use Project Explorer to examine the user interface for the sample document Document1.doc:
The Project Explorer window is highlighted in the programming environment. (If the window was not open, it will appear now.) You'll see a Project Explorer window that looks like this one:
You'll see a folder named Microsoft Word Objects and a folder named References.
Each project you work with that is based on a Word document or template will have a ThisDocument entry, which you can use to customize how your Word file opens and closes.
Word minimizes the Visual Basic development environment and displays the sample document (Document1).
The Properties window lets you change the characteristics, or property settings, of the Word objects, modules, and UserForms in your projects. A property setting is a quality of one of the components in your macro. For example, the Name property of the module in the Normal project that contains your InsertAddress macro is currently NewMacros. (Whenever you record a macro and place it in the Normal template, Word automatically places it in the NewMacros module.) If you'd like to change the name of this module to reflect its new contents, you can change it using the Properties window.
Inside the Properties window, you'll find an object drop-down list box, which you can use to switch between objects in the active project component. This list box is especially useful when the component you're working with contains multiple objects, such as a UserForm dialog box.
The main part of the Properties window is a two-tabbed properties list with scroll bars, which contains property settings that you can modify while your macro is being built (a construction phase programmers call design time). In the Properties window, you can view property settings alphabetically or by category. As you'll learn in the next chapter, properties can also be set while your macro is running (at run time) if you modify property settings appropriately using program code.
Take a moment now to change the Name property setting for the NewMacros module in the Normal template. By changing this setting from NewMacros to InsertAddress, you can clearly identify the contents of your address macro later, and your NewMacros module won't become too unwieldy. (You can keep all your macros in the NewMacros module if you like, but we recommend that you save at least some of them in separate modules.) Follow these steps:
The Properties window is highlighted in the programming environment. (If the Properties window wasn't open, it will appear now.) Your Properties window should look similar to the following:
The Properties window lists only one property for the NewMacros module, the Name property. Later you'll work with objects that contain dozens of property settings, but for now you get off easy.
The setting of the Name property changes from NewMacros to InsertAddress, and the new name appears in the Properties window, and in the Modules folder in Project Explorer.
When you have the Project Explorer window, the Properties window, and several other programming tools to contend with on the screen, the Visual Basic development environment can become a pretty busy place. To give you complete control over the shape and size of the elements in the development environment, Office gives you the ability to move, dock, and resize each of the programming tools.
To move a window, the toolbox, or the toolbar, you simply click the title bar and drag the object to a new location. If you align one window to the edge of another window, it will attach itself to the window, or dock. Dockable windows are advantageous because they always remain visible, and they have similar characteristics to the toolbars and menus in Office applications. (They won't become hidden behind other windows.)
If you want to see more of a docked window, simply drag one of the borders to see more content. If you get tired of docking and want your tools to overlap each other, choose Options from the Tools menu, click the Docking tab, and then remove the check mark from (deselect) each tool you want to stand on its own.
As you work through the following chapters, practice moving, docking, and resizing the different tools in the Visual Basic development environment until you feel comfortable with them.
The Visual Basic development environment comes with its own Help system that describes how to use the Visual Basic tools and how to write macros in each Office application. To get the complete Help system, you need to specify Visual Basic Help for each application when you run Setup. (These files aren't included in the standard installation of Office.) If you didn't specifically include these files, run Setup again now to install the necessary material.
You may also be able to install these files using Office's install-on-demand feature, which (if enabled) starts Setup automatically the first time you request Visual Basic Help and prompts you for the necessary Setup disc. As you learned in Chapter 4, install-on-demand is a special Setup option that conserves hard disk space, but you can't tell if it is active until you try to run the command.
Follow these steps to use the Visual Basic Help resources:
When you're finished working on a Visual Basic macro, you have two options: you can minimize the Visual Basic development environment and return to your Office application, or you can exit Visual Basic and return to your application. Unless you plan to edit another macro soon, you needn't leave Visual Basic running. If you want to use it again, you can easily restart it using the Macros dialog box.
To minimize Visual Basic and return to Office, do this:
To exit Visual Basic and return to Office, do this:
TIP
The keyboard shortcut for exiting Visual Basic and returning to your Office application is Alt+Q.