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 event-handling code to your application and review the sequence of the events.
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.
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 |
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 functions:
Working with Items That Contain VBScriptSometimes 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.