VBA Primer

  • 4/15/2011

Using the Visual Basic Editor

Before you start working with VBA code, take a few minutes to settle in to your surroundings. To help you work more comfortably, the subsections that follow describe a bit about each component of the Visual Basic Editor that is identified in Figures 23-2 (Word 2010) and 23-3 (Word 2011).

Figure 23-2

Figure 23-2 The Visual Basic Editor in Word 2010.

Figure 23-3

Figure 23-3 The Visual Basic Editor in Word 2011.

Introducing the Code Window

The code window is where your procedures appear. This is where you type macros when writing code and where you find the macros you’ve recorded. Notice that the procedure list is at the top-right of the code window. From this list, you can quickly move to any procedure in the active module.

Also notice the view options at the bottom of the screen. When you have several macros in a module, it can be helpful to view them one at a time. Full Module view is the default, but you can change this setting and many others through the Options dialog box (Preferences in Office 2011).

See Also For more about setting preferences for the Visual Basic Editor, see the section “Setting Up Your Workspace,” later in this chapter.

Introducing Project Explorer

Project Explorer is where you see the list of all VBA projects that are currently open or loaded. All open documents, as well as open or loaded document templates, appear here, whether or not they contain macros. You can collapse or expand a project to view the modules and objects that it contains.

A project has a Modules or Forms folder only if it contains code modules or UserForms. However, in Word and Excel, every project contains an Objects folder, such as the Microsoft Word Objects folder you see under each of the projects visible in Figures 23-2 and 23-3.

In Word, the Objects folder contains a document object referred to as ThisDocument. In Excel, it contains both a ThisWorkbook object and a sheet object for each existing sheet in the workbook. Some types of code (such as a type of procedure known as a document-level event) are added directly in the code window for the document object rather than in a module. However, you will often have projects that have no code added to the document objects.

See Also Learn more about using the document objects in the section “Introduction to Using Events,” later in this chapter.

Introducing the Properties Window

The Properties window shown in Figures 23-2 and 23-3 doesn’t look like much, but don’t be fooled. For modules, the Properties window is generally used only to edit the module name. However, for some object types (such as UserForms), the Properties window becomes extremely important because it’s populated with many settings that you can edit directly within it, ranging from the height and width of a UserForm to the value to display on a form control (such as a text box or an option button).

To edit the name of a module in the Properties window:

  1. Click into the name where it appears on either the Alphabetic or Categorized tabs.

  2. Edit it as you would document text.

    Module naming rules are the same as macro naming rules—no spaces or special characters, and the name must begin with a letter.

  3. Press Enter (Return) to set it.

Note that, when you record macros, they’re always added to a module named NewMacros. You can rename that module if you like, but the next time you record a macro, a new module will be created with the name NewMacros.

Setting Up Your Workspace

You’ll find many settings that can be customized in the Options dialog box, available on the Tools menu in the Visual Basic Editor. (In Office 2011, this is the Preferences dialog box, available from the application name menu, such as Word.) I don’t recommend spending much time in this dialog box just yet, because you might not be familiar with many of the settings. However, it’s good to know that it’s there, because you are likely to need it. This primer will point out when settings can be customized in this dialog box.

Possible settings in the Options (or Preferences) dialog box include default behavior for a number of programming actions (such as the way you’re notified about errors in your code), the formatting for each type of text or notification you see in the code window (such as comment text or errors), and the way the window itself is arranged.

In addition to settings in the Options dialog box, notice that you can drag to resize panes in the Visual Basic Editor window (such as the Project Explorer or Properties window), and can close those you don’t need. In the Visual Basic Editors for Office 2010, you can also drag to dock or float panes.

Use the View menu to access any windows you’ve closed. If you’re unable to dock any window in an Office 2010 Visual Basic Editor, you can change the setting for that window on the Docking tab of the Options dialog box.