Learning the Visual Basic Programming Tools

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.

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:

  • Menu bar
  • Toolbars
  • Project Explorer
  • Properties window
  • Online Help system

Using the Menu Bar

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:

  1. From the Run menu, choose Run Sub/UserForm. Word runs your macro again in the current document.

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

  2. Click the Microsoft Word program icon on the Windows taskbar. Windows restores Word and displays the current document.
  3. Verify that the InsertAddress macro correctly displayed your address text. If you've followed all the examples in this chapter, you should see four addresses, and the last one should have a new phone number.
  4. When you're ready to return to Visual Basic, click the Microsoft Visual Basic program icon on the Windows taskbar.

Run the macro again if you like, and take some time to experiment with other menus and commands.

Using the Visual Basic Toolbars

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.

click to view at full size.

Figure 37-2. The Standard toolbar provides the fastest way to issue commands in the Visual Basic development environment.

Using the Save Button to Save Your Changes

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:

  • Click the Save button on the Standard Visual Basic toolbar.

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.

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.

Using Project Explorer

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:

  • Word Objects, such as additional Word documents
  • Standard modules, which contain macros and other useful program code
  • Custom dialog boxes, called UserForms
  • Class modules, which define objects and the methods and properties used to control them
  • References to commands and objects in other Windows applications

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:

  1. Click the Project Explorer button on the toolbar.

    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:

  2. Click the plus sign (+) next to your sample document, Project (Document1) in this example, to view all the project's components.

    You'll see a folder named Microsoft Word Objects and a folder named References.

  3. Double-click the Microsoft Word Objects folder to open it, and then select the object named ThisDocument.

    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.

  4. To view the sample document's user interface, click the View Object button in the Project Explorer window.

    Word minimizes the Visual Basic development environment and displays the sample document (Document1).

  5. When you're finished viewing the sample document, click the Microsoft Visual Basic icon on the Windows taskbar to return to Visual Basic.
  6. Click the NewMacros module in the Normal project to switch back to the project that contains your InsertAddress macro.
  7. Click the View Code button in the Project Explorer window to display the program code for your macro in the Code window, if it isn't visible.

Using the Properties Window

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:

  1. Verify that the NewMacros module is selected in the Normal project.
  2. Click the Properties Window button on the toolbar.

    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.

  3. Double-click the NewMacros name in the Properties window, and press Delete to erase the current property setting.
  4. Type InsertAddress, and then press Enter.

    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.

Moving, Docking, and Resizing Tools

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.

Using the Help System

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:

  1. If the Visual Basic development environment is not already running, open it now by clicking the Tools menu, pointing to the Macros submenu, and clicking Visual Basic Editor.
  2. Click Microsoft Visual Basic Help on the Help menu.
  3. If the Help files are missing, you'll have the opportunity to install them now by using the Office Setup program. Click Install Help Files and place the required disc in your CD-ROM drive (typically Office 2000 CD 1).
  4. In the Office Assistant dialog box, type the Visual Basic concept, task, or keyword that you would like additional information about. The Office Assistant will present you with one or more dialog boxes of information about the topic you requested.

Exiting Visual Basic

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:

  • Click the Minimize button on the Visual Basic title bar. Visual Basic appears as an icon on the Windows taskbar, and Word reappears in a window.

To exit Visual Basic and return to Office, do this:

  • Click the Close button on the Visual Basic title bar. Visual Basic exits, and Word reappears in a window.

The keyboard shortcut for exiting Visual Basic and returning to your Office application is Alt+Q.

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