BeforeItemMove Not Triggered on Newly Created Outlook Folder

Add-in Express™ Support Service
That's what is more important than anything else

BeforeItemMove Not Triggered on Newly Created Outlook Folder
 
Joseph Acunzo




Posts: 13
Joined: 2012-01-03
The BeforeItemMove event is not triggered on a new folder that is created by the user after Outlook starts, but works fine on existing folders. Details of the problem follow.

At Outlook startup, I recurse through all Stores and all Folders in each store. For each folder, I add to a dictionary a new instance of OutlookItemsEvents and call ConnectTo (with True), using the EntryID as the dictionary key (just so later I can determine if it's already in the collection or not). Also for each folder, I add to another dictionary a new OutlookFoldersEvents instance and call ConnectTo (with True also).

The BeforeItemMove event is triggered when I move an item out of any of the folders that existed on startup. All works fine.

In the FolderAdd event, which I set a breakpoint on and do hit, I add to both of my dictionaries, using ConnectTo for each one.

I move an item into this new folder just created. I then move the item out of this new folder, but the BeforeItemMove event is not triggered. So it works on any of the folder that existed at startup, but not on a newly created folder. Why would it not work on the new folder?

This function is called on each folder, both at startup and for any newly created folder.


Public Sub TrapFolderEvents(folder As Outlook.MAPIFolder)
    Dim entryId As String = folder.EntryID

    If (Not m_OutlookItemsEvents.ContainsKey(entryId)) Then
        Dim oie As OutlookItemsEvents = New OutlookItemsEvents(Me)
        oie.ConnectTo(folder, True)
        m_OutlookItemsEvents.Add(entryId, oie)
    End If

    If (Not m_OutlookFoldersEvents.ContainsKey(entryId)) Then
        Dim fe As New OutlookFoldersEvents(Me)
        fe.ConnectTo(folder, True)
        m_OutlookFoldersEvents.Add(entryId, fe)
    End If
End Sub


Here is my BeforeItemMove event handler.


Public Overrides Sub BeforeItemMove(ByVal item As Object, ByVal moveTo As Object, ByVal SourceFolder As Object, ByVal e As AddinExpress.MSO.ADXCancelEventArgs)
        MsgBox("BeforeItemMove: " & CStr(item.Subject))
End Sub


Handle FolderAdd using this function


Public Overrides Sub FolderAdd(ByVal Folder As Object, ByVal SourceFolder As Object)
        AddinModule.CurrentInstance.TrapFolderEvents(CType(Folder, Microsoft.Office.Interop.Outlook.MAPIFolder))
End Sub
Posted 20 Feb, 2019 17:25:03 Top
Andrei Smolin


Add-in Express team


Posts: 18794
Joined: 2006-05-11
Hello Joseph,

In 99.9% cases, the COM object(s) supplied to a handler method of an Add-in Express event is released immediately after the event handler is done. This is because Add-in Express keeps track (or it should keep track) of *all* COM objects it creates; it releases some COM objects immediately, some other COM objects are released using a specific logic.

To deal with this, you should modify your code so that it creates another MAPIFolder object for your use. To do this, in TrapFolderEvents, you call Dim myFolder As Outlook.MAPIFolder = OutlookApp.Session.GetFolderFromID(entryId) and use myFolder instead of folder. This way, Add-in Express releases the original COM object and your code uses the COM object you create.


Andrei Smolin
Add-in Express Team Leader
Posted 21 Feb, 2019 06:43:47 Top
Joe Acunzo




Posts: 13
Joined: 2012-01-03
Thank you Andrei. Using your suggestion, the event is now being trapped on newly added folders.
Posted 21 Feb, 2019 11:52:20 Top
Andrei Smolin


Add-in Express team


Posts: 18794
Joined: 2006-05-11
You are welcome!


Andrei Smolin
Add-in Express Team Leader
Posted 22 Feb, 2019 00:44:09 Top