Adding an event handler to items programmatically

I am giving some of the items in my list to make the list a custom property, and I would like to fire a specific event when that property changes. Is there a way to write an event handler to handle events CustomPropertyChange

for all items in, olFolderToDo

or programmatically add an event handler to the items so that I can add an event handler when a new item is added to the folder?

I've handled this awkwardly in the past by putting my code in an event handler for a ItemsChange

for a folder and then checking the value of a property (for example, when I want to trigger an action after a task is marked complete, I watch for changes in items and then check whether the item is marked complete), but this does not handle arbitrary property changes and requires careful handling to avoid firing multiple times in a row.

An example of what I am currently doing:

Public WithEvents Items As Outlook.Items

Private Sub Application_Startup()
Set Items = Application.GetNamespace("MAPI").GetDefaultFolder(olFolderToDo).Items
End Sub

Private Sub Items_ItemChange(ByVal Item As Object)
If TypeOf Item Is Outlook.TaskItem Then
    If Item.Status = olTaskComplete Then
        DoTheThing
    End If
ElseIf TypeOf Item Is Outlook.MailItem Then
    If Item.FlagStatus = olFlagComplete Then
        DoTheThing
    End If
End If
End Sub

      

+3


source to share


1 answer


The CustomPropertyChange event is fired when a custom property of an element (which is an instance of the parent) changes. To be able to handle this event, you must subscribe to each item in Outlook separately, which is not a good idea. The best solution is to subscribe to the ItemChange event of the Items class, which is triggered when an item in the specified collection changes. In this case, you can control a folder, not a single folder. But he won't tell you which property has changed. However, you can save two custom properties to keep the values ​​in sync (the first for the source property value and the second for the old value, so you can figure out which property has changed and what the old value is).



As a workaround, you might consider using the low-level API that Outlook is based on - Extended MAPI. There you can find the fnevObjectModified notification. For more information, see Event Notification in MAPI . Note that you can use any wrappers around extended MAPI from managed code to access low-level notifications (like Redemption or MAPI Store Accessor ).

0


source







All Articles