Reputation: 2533
I am writing an Outlook add-in and I am hooking into the AttachmentAdd event of the mailItem. It works as expected when one email is composed but when more than one emails are being composed at the same time, it seems the AttachmentAdd event fires only for the first email to which the attachment is being added.
I am not sure if it has anything to do with the RCW going out of scope.
I am using Outlook 365 64 bit, if that helps.
Here is the code:
public partial class MyAddIn
{
private void OutlookAddIn_Startup(object sender, System.EventArgs e)
{
Application.ItemLoad += NewItem_Load;
}
private void NewItem_Load(object item)
{
Outlook.MailItem newMailItem = null;
newMailItem = item as Outlook.MailItem;
if (newMailItem != null)
{
newMailItem.AttachmentAdd -= MailItem_AttachmentAdd;
newMailItem.AttachmentAdd += MailItem_AttachmentAdd;
}
}
// This gets called only for one email.
private void MailItem_AttachmentAdd(Outlook.Attachment attachment)
{
// Do some stuff here.
}
private void OutlookAddIn_Shutdown(object sender, System.EventArgs e)
{
Application.ItemLoad -= NewItem_Load;
// Log
}
protected override Microsoft.Office.Core.IRibbonExtensibility CreateRibbonExtensibilityObject()
{
return new MyRibbon(this);
}
#region VSTO generated code
private void InternalStartup()
{
this.Startup += new System.EventHandler(OutlookAddIn_Startup);
this.Shutdown += new System.EventHandler(OutlookAddIn_Shutdown);
AppDomain.CurrentDomain.UnhandledException += CurrentDomain_UnhandledException;
}
}
Upvotes: 0
Views: 151
Reputation: 66286
You are setting up events the event handler on a local variable (newMailItem
), that will be released at some point after NewItem_Load()
exits.
The variable must stay alive to raise events. You could move the declaration to the class level, but you can have more than one open item. Try to create a wrapper class that holds MailItem
as its member and store that wrapper in a list.
Upvotes: 1