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