Outlook Item Change duplication

If ItemChange event triggers, it triggers, nothing you can do about it, unless you change the code behind ItemChange, which is unlikely.

But if you cannot change it, you can always control it. I tried to control it with LastModificationTime compared to current time but the trigger is sometimes instant so it did not work out well. Then I tried to control item's UserProperties which took me sometime to figure out, but eventually it worked. My code works with "Blue Category", so you can change it to "Blue" if it works for you.

Use the following:

Dim myProp As Outlook.UserProperty
Set myProp = Item.UserProperties.Find("MyProcess")

If Item.Categories <> "Blue Category" Then
    Debug.Print "Removing Blue Category and reseting Item Property"
    Set myProp = Item.UserProperties.Add("MyProcess", olText)
    myProp = True
    Exit Sub
End If

If TypeOf Item Is Outlook.MailItem And Item.Categories = "Blue Category" Then
    If myProp Is Nothing Then
        Debug.Print "Categorizing Item to Blue Category"
        Set myProp = Item.UserProperties.Add("MyProcess", olText)
        myProp = False
        Set objMail = Item
    ElseIf myProp = True Then
        Debug.Print "Re-categorizing Item to Blue Category"
        Set myProp = Item.UserProperties.Add("MyProcess", olText)
        myProp = False
        Set objMail = Item
    Else
        Debug.Print "Item has already been processed"
        Exit Sub
    End If
Else
    Debug.Print "Wrong category or action, exiting sub."
    Exit Sub
End If

instead of this:

If Item.Class = olMail And Item.Categories = "Blue" Then
    Set objMail = Item
Else
    Exit Sub
End If

Are you using the status flags on these e-mails? If you aren't using those for anything else you can just do something lazy like

Private Sub objMails_ItemChange(ByVal Item As Object)

    If Item.Class = olMail And Item.Categories = "Blue" Then
        Set objMail = Item
        If objMail.FlagStatus = olFlagComplete Then Exit Sub
        objMail.FlagStatus = olFlagComplete
    Else
        Exit Sub
    End If

And it will set set the e-mail with the checkmark flag the first time it reads with the blue category (and run your code) then ignore the e-mail every other time. There are plausibly better places to put the code in the first place then ItemChange but I'm not thoroughly familiar with all of Outlook's event callbacks.

Tags:

Excel

Vba

Outlook