HowTo: Avoid limitations of Microsoft Outlook ItemAdd event
The description of the ItemAdd event is laconic and compact – “Occurs when one or more items are added to the specified collection. This event does not run when a large number of items are added to the folder at once.” But what is really implied by the words “a large number of items”?
Our tests show that in Outlook 2000, 2002 and 2003 this event is not fired if more than 16 elements are dragged. Imagine, only 16! Outlook 2007 with SP2 is more stable in this regard, the ItemAdd event stops firing when more than 512 elements are dragged simultaneously. How can you get all the moved items? You need to use the Extended MAPI IMsgStore::Advise method and implement the IMAPIAdviseSink::OnNotify method.
All this has already been implemented in our MAPI Store Accessor and works just fine (BTW, it is a free tool and you can download it right now). In this particular case, it is the OnObjectMoved event that interests us most:
Private Sub adxmapiStoreAccessor_OnObjectMoved( _ ByVal sender As System.Object, _ ByVal e As AddinExpress.MAPI.ADXMAPIObjectNotificationEventArgs) _ Handles adxmapiStoreAccessor.OnObjectMoved Dim folder As AddinExpress.MAPI.Folder = _ adxmapiStoreAccessor.GetMapiFolder(itemsEvents.FolderObj) If folder IsNot Nothing Then Dim folderId As Object folderId = folder.GetProperty( _ AddinExpress.MAPI.ADXMAPIPropertyTag._PR_ENTRYID) folder.Dispose() If e.ParentId.ToString() = _ AddinExpress.MAPI.EntryId.ConvertToString( _ CType(folderId, Byte())) Then Dim ns As Outlook._NameSpace ns = OutlookApp.GetNamespace("MAPI") If ns IsNot Nothing Then Try Dim mail As Outlook.MailItem mail = TryCast( _ ns.GetItemFromID(e.EntryId.ToString(), _ Type.Missing), Outlook.MailItem) If mail IsNot Nothing Then Try AddEventToLog( _ " Object moved. Subject = " + _ mail.Subject) Finally Marshal.ReleaseComObject(mail) End Try End If Finally Marshal.ReleaseComObject(ns) End Try End If End If End If End Sub
MAPI store events have 2 specificities related to MS Exchange-based stores:
- Events are raised asynchronously, which means that it could be a significant time interval since the items were dragged before the event was triggered.
- The Public Folders store doesn’t support events at all.
Well, thanks for reading. Hope somebody finds it useful.
This sample add-in was developed using Add-in Express 2008 for Microsoft Office and .net