views:

453

answers:

2

Hey, 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.

A: 

If I understand your problem description above,

The event is firing if you move the Item from inbox to spam folder but it not working if you move the same item back. ?? as you have to hook the event for alls folder that you will move from.

what may be happening is that you may have to release the Object in your event handler Marshal.ReleaseComObject()

76mel
A: 

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.

alex83
Forgetting Marshal.ReleaseComObject() is a common problem when using COM and Outlook
76mel