VBA has an easy-to-use interface called the Visual Basic Editor (VBE). In order to access the VBE, you first need to display the Developer tab. To show the Developer tab, open a 2007 Office System application, for example Word 2007. Click the Microsoft Office Button, and then click Word Options. In the Top options for working with Word section of the Word Options dialog box, click Personalize. Select Show Developer tab in the Ribbon, as shown in the next figure. Click OK.
Once the Developer tab is displayed, you can access in Word by choosing Visual Basic (or press ALT+F11). If you’ve only been a Word user up until now, the first thing you will notice in Figure below is that the VBE doesn’t resemble anything close to a document. The VBE is a very powerful development environment.
The application window is divided into three subwindows when you first enter the VBE. On the right are the Project Explorer and Property windows. The main window is the Module window. This is where you’ll be writing the majority of your code. You can add other debugging windows to monitor variable values while you step through code. These debugging windows will be covered later.
VBE Component Basics
The Visual Basic Editor contains a number of development tools that were once found only in advanced development environments. Each of these components appears in a different window and each window may be resized, moved, or docked (double-click the left mouse button on the title bar or choose Tools|Options|Docking). If you need to view Help for an individual window within VBE, position your cursor within the window and press F1. Each of these windows is described below.
The main window that occupies the majority of the screen is the Module window, shown in Figure 4-1. This is where you enter the VBA code that lies behind Word template or document. The Module window displays the VBA code that is associated with the selected object. If you are viewing an object, you can display the Module window by double-clicking the object. For example, add a UserForm, then double-click the UserForm to display the Module window (the process of double-clicking will also automatically create a subroutine for the UserForm_DoubleClick event).
Object Window and Procedure List
At the top of the Module window are two drop-down menus. The drop-down menu on the left is called the Object List. It contains a list of all objects in the active module. For instance, when the active module is a UserForm, all of the controls on the UserForm will appear in the drop-down. Finally, most modules also contain a General section. The General section is a place where general declarations can be typed. We’ll cover general declarations later, but basically these include any option statements, such as Option Explicit (to force variable declaration) or Option Compare Text (to compare text rather than ascii character values). The drop-down list on the right is the Procedure and Event List. This will contain all of the available events for the current object.
In the upper-left corner is the Project Explorer window, shown in figure below. In Word, this window has a tree structure representing all open documents and templates. These documents and templates are projects as far as VBA is concerned. A project is basically a warehouse for Objects and the code behind them. You can click on any of the small plus signs to expand the branch; this will expose any of the five possible nodes: ThisDocument, UserForms, Modules, Class Modules, and References. In addition, there are two small buttons toward the top of the window that can be used to switch between viewing a form and its accompanying code.
The UserForms branch will contain any customized UserForms attached to the document or template. The term UserForm is often used interchangeably with dialog box (generally, in the VBA vernacular, a UserForm is one that is custom created, and a dialog box is one that is built into the program). It may help to think of a UserForm as a blank object that can be turned into a dialog box through the use of controls and code. Controls facilitate certain tasks by executing the code behind the control’s events. The actual UserForm is manipulated by changing its properties or adding controls from the toolbox (Edit|Toolbox).
Modules are basically storage areas for procedures (functions and subroutines). Variables may also be declared outside of procedures in the Modules section.
Class modules are a specialized form of a module that allow for the creation of an object. Class modules can be used in creative ways to capture events for many intrinsic objects that are dynamically created or accessed.
The References section here allows for one project (usually a document) to call procedures in another project (usually the template used to create that document). You will see that references are invaluable for using Office automation. In Word, all documents will contain a reference to the template that was used to create them.
Do not confuse a reference displayed in the Project Explorer with one that shows up in the Tools|References section. References in Word’s Project Explorer are generally other templates. References in the Tools|References section refer to the object libraries available to the project (usually DLL files).
The ThisDocument section contains the properties of the actual document or template. This section operates in a very similar manner to a standard code module. If you are familiar with any of the recent Word macro viruses, you probably have seen how they use this section to propagate.
The Properties Window
The Properties window appears immediately below the Project window. See Figure 4-4. This window displays the design time properties for the object that has the focus.
Word VBA Tip: The object may have other properties that do not show in the Properties window. Several objects have properties that are only available at run time.
As a general rule, you can think of properties as the adjectives of an object. They determine an object’s characteristics. Common properties include height, width, background color, font, etc. In addition to these simple properties, all controls have advanced properties that allow greater manipulation to achieve desired results.
Note: Some properties may be changed only at run time and thus will not show up in the Properties window, while others are read-only and do not allow the alteration of their values.
Popularity: 2% [?]