Adding Events to VBScript Behind Forms

By using the Script Editor, you can add code to an Outlook item event to modify the event's behavior. For example, for the Open event, you can add code to specify the current form page, or you can add code to load a ComboBox control with a recordset from a database. For the Click event, you can create a procedure that creates a custom item, includes an attachment to the current item, and then posts the item in a folder. Item events are available for all Outlook item types except for the Noteltem object. Certain events are cancelable; you can prevent them from completing by writing the appropriate code. The following table summarizes the item events in Outlook.

Event

Cancelable Description

AttachmentAdd AttachmentRead BeforeAttachmentSave BeforeCheckNames

No No Yes Yes

Before Delete (new to Outlook Yes 2002)

Close

CustomAction

CustomPropertyChange

Forward

Open

PropertyChange

Read

Reply

ReplyAII

Send

Write

Occurs when an attachment has been added to the item. Occurs when an attachment has been opened for reading. Occurs before an attachment in an item is saved.

Occurs before Outlook starts resolving names in the Recipients collection of the item.

Occurs before an item is deleted. This event will not fire unless the item is displayed in an Inspector.

Occurs before Outlook closes the Inspector displaying the item.

Occurs before Outlook executes a custom action of an item.

Occurs when a custom item property has changed.

Occurs before Outlook executes the Forward action of an item.

Occurs before Outlook opens an Inspector to display the item.

Occurs when an item property has changed.

Occurs when a user opens an item for editing.

Occurs before Outlook executes the Reply action of an item.

Occurs before Outlook executes the Reply To All action of an item.

Occurs before Outlook sends the item.

Occurs before Outlook saves the item in a folder.

To add or modify an event

1. Wth the form in Design mode, click View Code on the Form Design toolbar.

2. On the Script Editor Script menu, click Event Handler.

3. In the Insert Event Handler list box, double-click the event you want.

With an event procedure, the word item refers to the current Outlook item associated with the form. For example, the following ltem_PropertyChange procedure sets the value of the Subject field in the item when the value in the Sensitivity drop-down list box in the Message Options dialog box is changed:

Sub ltem_PropertyChange(ByVal PropertyName) Select Case PropertyName Case "Sensitivity"

Item.Subject = "The sensitivity value has changed." End Select End Sub

Item events are not raised exclusively at the item level in Outlook 2000 and Outlook 2002. A COM Add-in application event handler can also use the WithEvents declaration to raise item-level events and write item event procedure code in VBA or Visual Basic. However, if you want an event's code to travel with the form, you should use item-level event procedures written in VBScript.

The Firing Sequence of Events

When an Outlook form is opened to compose or read an item, events are fired in the sequences described in the following table.

Event

When Fired

Open

A form is opened to compose an item.

Send, Write, Close

An item is sent.

Write, Close

An item is posted.

Write

An item is saved.

Close

An item is closed.

Read, Open

An item is opened in a folder.

Reply

A user replies to an item's sender.

ReplyAII

A user replies to an item's sender and all recipients.

Forward

The newly created item is passed to the procedure after the user selects the item's Forward action.

PropertyChange

One of the item's standard properties is changed.

CustomPropertyChange

One of the item's custom properties is changed.

CustomAction

A user-defined action is initiated.

AttachmentAdd

An attachment is added to a message. This event occurs before the BeforeAttachmentSave event (described below).

AttachmentRead

An attachment is read.

Before Delete

This event occurs before an item is deleted. The item must be open in an Inspector for this event to fire.

BeforeAttachmentSave

An attachment is saved in a message. This event occurs after the Send event and before the Write event.

BeforeCheckNames

Recipient names are resolved. This event occurs before the Send event.

An item's events fire whether the item is created through program code (in VBScript or VBA) or through the Outlook user interface.

The order in which Outlook calls event handlers might change depending on other events that can occur. The order might also change in future versions of Outlook.

Creating a New Item

When you create a new item, the ltem_0pen event is fired. In Outlook, you generally create a new item by opening a form. However, for certain folders, such as the Tasks folder, you can create a new item by clicking the New Item row in the folder. In either case, the ltem_0pen event is fired.

Sending an Item

When you send an item, the ltem_Send event is fired, followed by the ltem_Write event and then the ltem_Close event. Posting or Saving an Item

In Outlook, posting an item achieves the same result as saving an item. When you post or save an item, the ltem_Write event is fired. An ltem_Write event can occur in several situations. It can occur when a Post item is created and the Post button is clicked on the form, when a Contact item is created and the Save And Close button is clicked on the form, or when a Task item is created by clicking the New Item row and the item is then saved by the user clicking outside the New Item row. After the ltem_Write event fires, the ltem_Close event

Opening an Existing Item

In Outlook, an item can be opened in two ways. If the item exists in a view that allows in-cell editing, simply clicking the item in the view will open it. An existing item can also be opened by double-clicking it and viewing it in a form. In either case, the ltem_Read event is fired. If the item is viewed in a form, the ltem_Read event is followed by the ltem_0pen event.

Sometimes you will not want the ltem_0pen event to fire when an item is opened by simply being clicked in a view with in-cell editing or by another process. Outlook does not provide an ltem_New event. The following ltem_Open procedure provides an escape hatch for exiting the procedure if the item is not new:

Sub ltem_Open

'1/1/4501 represents a date field with no value 'If the item is not new, exit sub If Item.CreationTime <> #1/1/4501# Then

Exit Sub End If

'Continue ltem_Open code here End Sub

Closing an Item

When you close an item, the ltem_Close event is fired. If changes have been made to the item, when you attempt to close the item, Outlook will ask if you want to save the changes. If you click Yes, the ltem_Write event is fired, followed by the ltem_Close event.

Was this article helpful?

0 0

Post a comment