Outlook 2007 add-in – Problem using BeforeItemMove event

add-inoutlookoutlook-2007outlook-addinvsto

I am writing an Outlook 2007 addin. All I am doing is:

        private void ThisAddIn_Startup(object sender, System.EventArgs e)
    {
        Outlook.Folder root;

        //creates Spam folder if it dosen't exist
        if (!SpamFolderExist())
        {
            CreateSpamFolder();
        }

        root = (Outlook.Folder)this.Application.Session.DefaultStore.GetRootFolder();

        //set BeforeItemMove event for spam and inbox folders
        spamFolder = (Outlook.Folder)root.Folders["Spam"];
        inboxFolder = (Outlook.Folder)this.Application.ActiveExplorer().Session.GetDefaultFolder(Outlook.OlDefaultFolders.olFolderInbox);

        spamFolder.BeforeItemMove += new Microsoft.Office.Interop.Outlook.MAPIFolderEvents_12_BeforeItemMoveEventHandler(BeforeItemMoveFromSpam);
        inboxFolder.BeforeItemMove += new Microsoft.Office.Interop.Outlook.MAPIFolderEvents_12_BeforeItemMoveEventHandler(BeforeItemMoveFromInbox);

        //set new mail event
        this.Application.NewMail += new Microsoft.Office.Interop.Outlook.ApplicationEvents_11_NewMailEventHandler(OnNewMail);
    }

And the problem is that, even if I am writing nothing in BeforeItemMoveFromInbox and BeforeItemMoveFromSpam methods, the application has a strange behavior. After I am moving some mails, it just not performs any more move action for a particular mail. It seems that a mail is blocked and I just can’t move it. After performing other moving actions other mails are blocked and the one that was previously blocked can be moved. The idea is that after a mail is blocked it will always be at least one mail that cannot be moved. In other words randomly some of moving actions fail. I have to say that I am not doing anything else than moving mails from a folder to another and that I get no error message. I also tried to set the cancel parameter of BeforeItemMove event handlers to false just at the end of the methods but I got the same behavior.

Best Answer

You got it 76mel. I had the same problem (just hooking up a folder to the BeforeItemMove event made it so that when I moved a message out of that folder, I couldn't move it from the new folder to yet another folder for a small amount of time).

Adding Marshal.ReleaseComObject() to the end of the event handler fixed it perfectly.

Related Topic