Collectives™ on Stack Overflow
Find centralized, trusted content and collaborate around the technologies you use most.
Learn more about Collectives
Teams
Q&A for work
Connect and share knowledge within a single location that is structured and easy to search.
Learn more about Teams
Ask Question
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;
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.
–
–
–
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.