views:

368

answers:

1

Is it possible in Microsoft Outlook VBA to catch the Open event of any mail item that gets opened? I'd like to add a category label to any mail item I have opened, to have an alternative 'unread' option I could script against for something else. I've tried this:

Private Sub MailItem_Open()
    MsgBox "test"
End Sub
+1  A: 

Perhaps something on the lines of:

Public WithEvents myOlInspectors As Outlook.Inspectors
Public myInspectorsCollection As New Collection

Private Sub Application_Startup()
    Initialize_handler
End Sub

Public Sub Initialize_handler()
    Set myOlInspectors = Application.Inspectors
End Sub

Private Sub myOlInspectors_NewInspector(ByVal Inspector As Outlook.Inspector)
If (Inspector.CurrentItem.Class = olMail) Then

    If Inspector.CurrentItem.Parent = "Inbox" Then
        strCats = Inspector.CurrentItem.Categories

        If InStr(strCats, "Read") = 0 Then
            If Not strCats = vbNullString Then
                strCats = strCats & ","
            End If
            strCats = strCats & "Read"
            Inspector.CurrentItem.Categories = strCats
            Inspector.CurrentItem.Save
        End If
    End If
End If
End Sub

The above should go in ThisOutlookSession. You will need to ensure that your security levels allow macros.

Remou
Thanks, I was trying to catch the ItemChange event, but I didn't know about the NewInspector event. It does suit better. I've added If Inspector.CurrentItem.Parent.FolderPath = "\\Mailbox - support\Inbox" Thento limit it to a second mailbox I have open.
Stijn Sanders