Application-Level Events
This section covers events that occur at the Application level. This includes either events raised on the Application object or events that are raised on the main Outlook windows. The two primary windows displayed by Outlook are represented in the Outlook object model by the Explorer object and the Inspector object. An Explorer object represents the main Outlook window in which the contents of folders display. An Inspector object represents the Outlook window that appears when you double-click an Outlook itemfor example, when you double-click a mail item in your inbox. Figure 10-1 shows representative Explorer and Inspector windows.
Figure 10-1. An Explorer window and an Inspector window.
[View full size image]

Startup and Quit Events
Outlook raises several events during startup and shutdown:
- Application.Startup is raised when Outlook has completely started up. This event is raised after add-ins have been loaded so that an add-in can handle this eventthat is, it is not raised before add-ins are loaded (as are some events in Word and Excel).
- Application.MAPILogonComplete is raised after Outlook has logged on to the mail services it is configured to connect to.
- Application.Quit is raised when Outlook is about to exit. This event is raised before add-ins have been unloaded so that an add-in can handle this event. A VSTO Outlook add-in is unloaded before the Quit event is raised and should use the Shutdown event instead.
Quit is the name of both a method and an event on the Application object. Because of this collision, you will not see the Quit event in Visual Studio's pop-up menu of properties, events, and methods associated with the Application object. Furthermore, a warning displays at compile time when you try to handle this event. To get Visual Studio's pop-up menus to work and the warning to go away, you can cast the Application object to the ApplicationEvents_11_Event interface, as shown in Chapter 23, "Developing COM Add-Ins for Word and Excel") and Outlook's Startup, Quit, and MAPILogonComplete events occur is shown here:
Listing 10-1 shows an add-in that handles these three events. It also displays message boxes when the methods of IDTExtensibility2 are called.
Listing 10-1. A COM Add-In That Handles the Application Object's Quit, Startup, and MAPILogonComplete EventsThe order in which a VSTO Outlook add-in's Startup and Shutdown event handlers and Outlook's Startup, Quit, and MAPILogonComplete events occur is shown here:
Activation EventsWhen an Explorer or Inspector window becomes the active window (activates) or loses focus to another window (deactivates), events are raised:
Listing 10-2 shows a VSTO Outlook add-in that handles Activate and Deactivate events for the Explorer object.
Listing 10-2. A VSTO Add-In That Handles the Explorer Object's Activate and Deactivate Events
New Window EventsWhen a new Explorer or Inspector window is created, Outlook raises an event:
Listing 24-1 shows an example of handling these events. Window EventsWhen an Explorer or Inspector window is maximized, minimized, moved, or resized, events are raised by Outlook. All of these events can be cancelled to prevent the change to the window from occurring:
Close EventsWhen an Explorer or Inspector window is closed, Outlook raises an event:
View and Selection Change EventsAs you navigate from folder to folder in an Explorer window, Outlook displays a view of the items in the folder you have selected. The user can also change the view for a particular folder by using the View menu and choosing a different view from the Current View menu in the Arrange By menu. Outlook raises events when the view changes or the selection changes:
Listing 10-3 shows a VSTO Outlook add-in that handles these events. Listing 10-3. A VSTO Add-In That Handles View and Selection Change Events
Folder Change EventsGiven a collection of folders in Outlook, several events are raised when folders in that collection change:
Listing 10-4 shows an add-in that handles folder change events for any subfolders under the Inbox folder. To get to a Folders collection, we first get a NameSpace object. The NameSpace object is accessed by calling the Application.Session property. The NameSpace object has a method called GetDefaultFolder that returns a MAPIFolder object to which you can pass a member of the enumeration OlDefaultFolders to get a standard Outlook folder. In Listing 10-4, we pass olFolderInbox to get a MAPIFolder for the Inbox. We then connect our event handlers to the Folders collection associated with the Inbox's MAPIFolder object. Listing 10-4. A VSTO Add-In That Handles Folder Change Events
|