Preventing Events from Firing

In some cases, you might want to prevent events from occurring. For example, you might want to add code for a Reply event that opens a custom form rather than the default Message form. To achieve this functionality, you must first prevent the default behavior from occurring. To do so, assign False to the function value. The actual Event procedure must be declared as a Function procedure instead of a Sub procedure so that you can cancel an Item event with an ltem_EventName = False statement.

VBScript and VBA use different syntax to cancel events. In VBScript code, you write the event procedure as a function and set the function value to False in order to cancel the event. In VBA or Visual Basic code, the event procedure contains a Cancel argument. Set the Cancel argument to True to cancel the event. The following VBScript example prevents the standard Reply event from occurring in a Mail Message form. Instead, it opens a custom Orders form when the Reply button is clicked.

Function ltem_Reply(ByVal Response) ltem_Reply = False

Set MyFolder = Application.GetNameSpace("MAPI").GetDefaultFolder(6) Set Myltem = MyFolder.ltems.Add("IPM.Note.Orders") Myltem.To = Item.To

Myltem.Subject = "RE: " & Myltem.Subject Myltem.Display End Function

The following example shows the same code located in an application-level event procedure in VBA. Unlike Outlook 97 and Outlook 98, Outlook 2002 allows you to raise events at the form level using the correct WithEvents declarations.

Public WithEvents objMailltem As Outlook.Mailltem 'Additional code to instantiate objMailltem object as Inspector.Currentltem Private Sub objMailltem_ReplyAII(ByVal Response As Object, Cancel As Boolean) Cancel = True Set objFolder = _

objApp.GetNameSpace("MAPI").GetDefaultFolder(olFolderlnbox) Set objltem = objFolder.ltems.Add("IPM.Note.Orders") objltem.To = objMailltem.To objltem.Subject = "RE:" & objltem.Subject objltem. Display End Sub

Form-level events that can be canceled include the following: ® ltem_BeforeAttachmentSave

• ltem_BeforeCheckNames ® ltem_BeforeDelete

• ltem_CustomAction ® ltem_Forward

Many other cancelable events are available at the application level. If you want to respond to application-level events, you must write VBA code in ThisOutlookSession or create a COM Add-in.

Was this article helpful?

0 0

Post a comment