Although this chapter will discuss attaching code to Outlook forms, rather than creating and modifying Outlook forms themselves, we'll begin by looking at how the Outlook object model views the Outlook user interface and by briefly examining how you access and work with Outlook forms in design mode; both topics provide background that is necessary in order to begin coding. Then we'll look at Outlook's rather primitive VBScript environment.
6.2.1 Interfaces and Objects
Figure 6-1 shows a more-or-less standard Outlook window with Outlook displaying a mail folder. The Outlook window is divided into three parts, which correspond to four elements of the Outlook object model.
Figure 6-1. The Microsoft Outlook interface
The Folder List
On the left of the Outlook window is the Folder List. In the Outlook object model, this corresponds to the NameSpace object, which has a Folders collection in which each Folder object represents a folder in the MAPI store.
The Explorer
On the upper right of the Outlook window is theExplorer pane. (The term "Explorer" here is unrelated to Windows Explorer, the utility for displaying the Windows namespace and filesystem.) The Explorer pane is responsible for listing the items in the current folder. Each type of item has its ownExplorer object, which is a member of the Explorers collection.
The Inspector
On the lower right of the Outlook window is thePreview pane. In other cases, when the entire right side of the Outlook window is occupied by the Explorer pane, the Preview pane appears when the user selects an item in the Explorer pane. The Preview pane is responsible for displaying the item selected in the Explorer pane and corresponds to anInspector object in the Outlook object model. Note that the Inspector object uses an Outlook form to present a particular view of an Outlook data item.
An item
An item is one of several different object types that hold information. Outlook items include mail messages, appointments, and contacts. In the Outlook object model, these correspond to objects of specific kinds. For instance, a mail message is represented by a MailItem object, while an appointment is represented by an AppointmentItem object and a contact is represented by a ContactItem object. Table 6-1 lists the items available in Outlook and their corresponding objects in the Outlook object model.
Item type |
Outlook object |
---|---|
appointment |
AppointmentItem object |
contact |
ContactItem object |
distribution list |
DistListItem object |
document |
DocumentItem object |
journal entry |
JournalItem object |
mail message |
MailItem object |
mail nondelivery report |
ReportItem object |
meeting |
MeetingItem object |
note |
NoteItem object |
post |
PostItem object |
remote mail message |
RemoteItem object |
task |
TaskItem object |
task acceptance notification |
TaskRequestAcceptItem object |
task rejection notification |
TaskRequestDeclineItem object |
task assignment request |
TaskRequestItem object |
task assignment update |
TaskRequestUpdateItem object |
With this basic (and frequently nonintuitive) terminology out of the way, we return to the discussion of accessing the environment for developing Outlook forms.
6.2.2 Outlook Form Design Mode
Outlook requires that a form be in design mode rather than in run (or display) mode before you can attach code to it. You can select the form that you'd like to program and open it in run mode in any of the following ways:
You can then place the form in design mode by selecting Tools Forms Design This Form from the form's menu.
You can also open a form and place it in design mode in either of the following ways:
Since Outlook's form-based development environment is somewhat idiosyncratic, let's review some of the basics of working with Outlook forms:
The Standard Forms library
These are the "out of the box" forms provided by Outlook. The library is read-only; if you modify its forms, you have to save them elsewhere.
The Personal Forms library
These are customized forms stored in the current user's personal store (.pst) file. As a result, they are inaccessible to other users.
The Organizational Forms library
For organizations using Microsoft Exchange, these forms are stored on the server and are accessible to all Outlook users with access to the server and with the necessary permissions. The library is unavailable for Outlook clients not using Microsoft Exchange.
An Outlook folder
Forms stored in Outlook folders are accessible only in that folder. If the folder is a public one, then the form is available to all users with access to the folder. Otherwise, the form is stored in the user's .pst file and is accessible only to him.
6.2.3 The VBScript Environment
To write code for your form, open the VBScript editor by clicking on the View Code button on the form's toolbar or select the View Code option from the form's Form menu. Outlook will open the VBScript editor, which is shown in Figure 6-2.
Figure 6-2. The VBScript editor
If you're familiar with the rich development environments of Visual Basic or the hosted versions of VBA, you'll recognize the VBScript editor as an extremely poor cousin. In fact, the editor is distinctly Notepad-like, without any of the amenities of the VBA IDE. Syntax is not checked automatically, nor are auto list members, auto quick info, or auto data tips available. In fact, the editor does not even have an option that allows you to require variable declaration. This feature, which is available in the VB and VBA IDEs, automatically adds an Option Explicit statement to each code module.
In keeping with its minimalist approach, the VBScript editor offers an object browser, though it lacks most of the ease-of-use features of the Object Browser found in the VBA IDE. To open it, select Script Object Browser from the VBScript Editor's menu. The VBScript object browser is shown in Figure 6-3. It lacks the icons that help identify elements in the VBA Object Browser. (For instance, the entries that begin "Ol" in the Classes list box are enumerations; casual inspection might lead you to completely overlook that fact.) Nor is the VBScript object browser searchable, which is a serious limitation. Finally, in the case of form-level events (which the object browser depicts as members of the ItemEvents object), the prototypes displayed by the object browser in the status bar are not completely consistent with the shells that the editor creates for them. For experienced programmers, this discrepancy will most probably go unnoticed. For inexperienced programmers, it serves as another source of needless confusion.
Figure 6-3. The VBScript object browser
If you need to browse the Outlook object model while you're programming, it's best to use the VBA Object Browser. It's available by opening the VBA IDE (select Tools Macro Visual Basic Editor from the Outlook menu) and pressing F2.
Part I: The Basics
Introduction
Program Structure
Data Types and Variables
Error Handling and Debugging
VBScript with Active Server Pages
Programming Outlook Forms
Windows Script Host 5.6
VBScript with Internet Explorer
Windows Script Components
Part II: Reference
Part III: Appendixes
Appendix A. Language Elements by Category
Appendix B. VBScript Constants
Appendix C. Operators
Appendix E. The Script Encoder