Outlook 2007 add-in - problem using BeforeItemMove event
I am writing Outlook 2007 addin. All I do 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 don't write anything in the BeforeItemMoveFromInbox and BeforeItemMoveFromSpam methods, the application has strange behavior. After I move a few emails, it just doesn't take any action to move specific mail. The mail seems to be blocked and I just can't move it. After performing other move actions, other messages are blocked, and the one that was previously blocked can be moved. The idea is that after mail is blocked, there will always be at least one mail that cannot be migrated. In other words, randomly, some moving actions fail. I must say that I am not doing anything other than moving emails from folder to another, and that I am not getting an error.I also tried setting the cancellation option of the BeforeItemMove event handlers to false only at the end of the methods, but I got the same behavior.
source to share
You got this 76mel. I had the same problem (just connecting a folder to the BeforeItemMove event resulted in when I moved a message out of that folder, I couldn't move it from the new folder to another folder for a short amount of time),
Adding Marshal.ReleaseComObject () to the end of the event handler is fixed.
source to share
If I understand your description of the problem above,
The event is triggered if you move an item from your inbox to your spam folder, but it doesn't work if you move the same item back. ?? since you need to intercept the event for the entire folder you will navigate from.
what can happen is that you may need to release the object in the Marshal.ReleaseComObject () event handler
source to share