Programming Microsoft Outlook and Microsoft Exchange, Second Edition (DV-MPS Programming)
Outlook supports 11 events that are called when users of your Outlook applications try to use some of the built-in capabilities of Outlook. You can write code to handle these events as well as to disable these events. Let's look at how to add some event-handling code to your application and review the sequence of the events.
Writing Event Handlers
The Outlook Script Editor makes it easy to add event handlers to your VBScript code. In the Script Editor, select Event Handler from the Script menu. Select the event you want to write a handler for, and click Add. Outlook will automatically add a skeleton function with the correct parameters for you. All you need to do is fill in your specific code to handle the event. The Script Editor does not, however, provide the option to add skeleton code to handle the events your controls support. In this case, you will need to write your event handlers from scratch. In Outlook, the event names are preceded with the word Item. For example, the open event handler is named Item_Open.
Disabling Events
You can prevent events and their default behavior from occurring by setting the function value, or the name of the event, to False. You can then place your own code inside the event handler to replace the standard behavior. For example, if you write a custom Contact form that synchronizes the information with a database, you can disable the Write event when the database is not available. This will prevent Outlook from saving the item and also prevent the two data sources, Outlook and the database, from becoming out of sync. The following code sample shows an example of this:
Function Item_Write() 'Call the CheckDatabase function boolIsOnline = CheckDatabase() if boolIsOnline = False then 'No database. Do not save the item. Item_Write = False msgbox "The database is offline" end if End Function Function CheckDatabase 'You would put your database connection code here CheckDatabase = False End Function |
Sequence of Events
Here is a list of the built-in Outlook events, which are discussed in more detail in the "Programming Outlook and Exchange Supplement" on the companion CD:
|
|
|
|
|
|
|
|
|
|
|
Outlook also includes another event, named Click, that can be used with your custom controls. The Click event is the only control event supported in Outlook. The following list describes the sequence of some of these events when you perform common Outlook tasks:
- Creating a new item using a form or in-cell editing. When a user attempts to create a new item in Outlook either by clicking the new mail icon to open a form or by starting to type information into a new item row in a view with in-cell editing enabled, Outlook will fire the Item_Open event.
- Sending an item. When a user attempts to send an item in Outlook, the Item_Send event is fired first followed by the Item_Write event and then the Item_Close event. If you disable any event in the sequence, the other events will not fire.
Working with Items That Contain VBScript
Sometimes you'll want to open your Outlook form without executing the VBScript code contained in the form. To do this, hold down the Shift key while opening the form. This method of opening forms is useful while designing your applications because it prevents VBScript functions from adding undesirable data into the form.
- Posting an item. When a user attempts to post an item, the Item_Write event is fired and then the Item_Close event is fired. If you disable any event in the sequence, the subsequent events will not fire.
- Saving an item. When a user tries to save an item, the Item_Write event is fired.
- Opening an item. When a user opens an item, the Item_Read event is fired followed by the Item_Open event.
- Closing an item. When a user attempts to close an item, the Item_Close event is fired.