Special Edition Using Microsoft Office Outlook 2003

Before you start developing in VBA, you should familiarize yourself with the various components of the VBA development environment (see Figure 33.1).

Figure 33.1. The VBA development environment contains several panes and toolbars.

Some of the VBA windows and menus are dockable, meaning that they can be moved and attached to the top, bottom, left, or right of the screen. Others operate as multiple document interface windows, with multiple windows displayed in one master window.

Using the Project Explorer

The Project Explorer contains all the modules, forms, and class modules in your project. Items are grouped by type. You can display the code associated with any of these items simply by double-clicking it.

The top level of the Project tree is the Microsoft Outlook Objects branch. There is always one object in this branch: the ThisOutlookSession object. This is where you place code that works at the application level.

The next branch of the Project tree is Forms. This branch holds any VBA forms you've created for your application.

Next is the Modules branch, which contains general code that isn't specifically related to an application-level event. If you have code that needs to be available to multiple areas of the application, place it in a module. You can also program procedures and functions in a module to run in response to a click of a toolbar button.

You can also create multiple modules in your project. For instance, if you have a set of functions or procedures that operate only on calendar items, you might want to create a calendar module to hold all of this code. That way, when you need to access one of these procedures, you know exactly where it's stored. When you create a new module, name the module with the prefix bas. This prefix has historically been used to name standard modules.

The last branch of the tree contains class modules. These modules can contain a variety of different functions. Unlike standard modules, however, class modules require that an instance of the class be created before any of the functions within can be used. Class modules are typically named with the prefix CLS.

To add a new file to your project, right-click anywhere in the Project window and select Insert from the context menu. From here you can insert a userform, module, or class module. You can then rename the new file using the Properties window, which is usually located immediately below the Project window. If you can't see the Properties window, select View, Properties Window to display it.

Using the Properties Window

The Properties window enables you to view and change any editable properties of any selected object. You can rename forms, modules, and class modules, or configure any controls on a VBA userform. Figure 33.2 shows the Properties window for a combo box in a VBA userform.

Figure 33.2. A combo box has many properties you can change.

Debugging Tools

There are three main tools you can use within VBA to debug your code:

  • Immediate window Enables you to view values and code status while your code is in break mode. Simply type ? variablename and click Enter to display the value of the variable.

  • Watches window You can create multiple watches in VBA. A watch is an expression you create and evaluate as your code is executed. For instance, you can create a watch to inform you when a certain item count is attained.

  • Locals window The Locals window enables you to view the values of all variables.

VBA Toolbars

Outlook's VBA development environment contains a number of toolbar buttons designed to help with the most common tasks in VBA. For more information about the specific toolbar buttons you might find helpful, see Table 33.1.

Table 33.1. VBA Toolbar Buttons

Icon

Command

Description

Run

Executes the current procedure or form

Reset

Stops the current code and resets the project

Break

Halts code execution at the current statement

List Properties/Methods

Opens a list that contains properties and methods for the current object in the code window

List Constants

Opens a list of constants for the current property in the code window

Comment Block

Changes the currently selected code into a comment

Uncomment Block

Changes the currently selected comment into code

Align

Aligns all selected controls

Center Horizontally

Centers all selected controls horizontally

Center Vertically

Centers all selected controls vertically

Make Same Width

Makes all selected controls the same width

 

Make Same Height

Makes all selected controls the same height

 

Make Same Height and Width

Makes all selected controls the same height and width

Категории