3

I'm giving some of the items in my to do list a custom property, and I'd like to perform a certain event when that property changes. Is there a way to either write an event handler to handle the CustomPropertyChange event for all items in olFolderToDo, or programmatically add an event handler to items so I can add the event handler when a new item gets added to the folder?

I've clumsily handled this in the past by putting my code in an event handler for ItemsChange for the folder and then checking the value of the property (for example, when I want to fire an action once a task is marked complete, I watch for changes in the items and then check if the item is marked completed), but this doesn't handle arbitrary changes to properties, and requires careful treatment to avoid firing multiple times in a row.

Example of what I currently do:

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
Martijn Pieters
  • 1,048,767
  • 296
  • 4,058
  • 3,343
Empiromancer
  • 3,778
  • 1
  • 22
  • 53

1 Answers1

0

The CustomPropertyChange event is fired when a custom property of an item (which is an instance of the parent object) is changed. To be able to handle that event you must subscribe to each item in Outlook separately which is not a good idea. Better solution is to subscribe to the ItemChange event of the Items class which is fired when an item in the specified collection is changed. In that case you can monitor the folder, not a single folder. But it will not tell you what property is changed. However, you can keep two custom properties for syncing values (the first for the source property value and the second for the old value, thus you will be able to figure out what property is changed and what is the old value).

As a workaround you may consider using a low-level API on which Outlook is based on - Extended MAPI. There you can find the fnevObjectModified notification. See Event Notification in MAPI for more information. Note, you can use any wrappers around Extended MAPI from a managed code to get access to low-level notifications (for example, Redemption or MAPI Store Accessor) .

Eugene Astafiev
  • 47,483
  • 3
  • 24
  • 45