Exchange 2010 and Outlook 2003: ItemAdd event does not fire in online mode


A known issue that keeps cropping up is one that occurs when running Outlook 2003 in online mode against Exchange 2010.  If you subscribe to events on an Items collection (e.g. you want to receive an ItemAdd event anytime an item is added to a folder), you will find that you do not actually receive the events when running in online mode.

The quickest fix for this is to change to cached mode - events are handled in a different way in cached mode, and the problem doesn't occur.

Of course, it isn't always possible to use cached mode.  There is another workaround that can be used quite successfully though.  Consider the following code (which can be tested in Outlook, just paste it into the ThisOutlookSession module of the VBA Editor):

Private WithEvents ReceivedItems As Items
Private WithEvents SentItems As Items

Private Sub Application_Startup()
    Set ReceivedItems = Application.GetNamespace("MAPI").GetDefaultFolder(olFolderInbox).Items
    Set SentItems = Application.GetNamespace("MAPI").GetDefaultFolder(olFolderSentMail).Items
End Sub

Private Sub ReceivedItems_ItemAdd(ByVal Item As Object)
    On Error Resume Next
    Item.Subject = "ItemAdd fired: " & Item.Subject
    Item.Save
End Sub

Private Sub SentItems_ItemAdd(ByVal Item As Object)
    On Error Resume Next
    Item.Subject = "ItemAdd fired: " & Item.Subject
    Item.Save
End Sub

The above code demonstrates the issue, and will not work in Outlook 2003 in online mode against Exchange 2010.  However, the following code does work:

Private WithEvents ReceivedItems As Items
Private WithEvents SentItems As Items

Private Sub Application_Startup()
    Set ReceivedItems = Application.GetNamespace("MAPI").GetDefaultFolder(olFolderInbox).Items
    Set SentItems = Application.GetNamespace("MAPI").GetDefaultFolder(olFolderSentMail).Items
    If MsgBox("ItemAdd hooks set. Implement fix?", vbInformation + vbYesNo, "Event Test Initialised") = vbYes Then
        ' Events may not fire against Exchange 2010
        On Error Resume Next
        ReceivedItems.GetFirst
        SentItems.GetFirst
        Err.Clear
    End If
End Sub

Private Sub ReceivedItems_ItemAdd(ByVal Item As Object)
    On Error Resume Next
    Item.Subject = "ItemAdd fired: " & Item.Subject
    Item.Save
End Sub

Private Sub SentItems_ItemAdd(ByVal Item As Object)
    On Error Resume Next
    Item.Subject = "ItemAdd fired: " & Item.Subject
    Item.Save
End Sub

 The fix is to call GetFirst on the folder you are subscribing to.  While the issue does seem to be due to a change in Exchange 2010, it is not likely to be fixed in Exchange as the workaround above is very easy to implement.  Later versions of Outlook subscribe to folders in a different way, and they are not affected by this issue.

Comments (1)

  1. Robin says:

    Hi,

    Thanks for the code snippet.

    I have another problem with my outlook COM addin DLL.

    my outlook is 2003 and Exchange Server is 2010.

    The following code is always returning result as 0

    Public g_InboxSch As Outlook.Search

    strF = AddQuotes("urn:schemas:httpmail:datereceived") & " > '" & g_dtInboxLastProcessedDate & "'"

    strS = oFolderInbox '"Inbox" 'search Inbox and all subfolders

    Set g_InboxSch = Nothing

    Set g_InboxSch = g_olApp.AdvancedSearch(Scope:=strS, Filter:=strF, SearchSubFolders:=True, Tag:="Complete")

    Result = g_InboxSch.Results.Count

    This issue is only happening if I connect my outlook 2003 to Exchange 2010.

    If its connected to Exchange 2007, its working fine and returning the expected result.

    Please help me…..

Skip to main content