Nicolas Brodnan
Nicolas Brodnan

Reputation: 35

MailItem event not firing without Visual Studio

I want to handled the BeforeAttachmentAdd event from an Outlook mail item. But my code works in Visual Studio environment but not out. Have you an idea?

This is my code:

namespace MyOutlookProject
{
   using Microsoft.Office.Interop.Outlook;
   using OutlookApplication = Microsoft.Office.Interop.Outlook.Application;
   using OutlookAttachment = Microsoft.Office.Interop.Outlook.Attachment;
   using OutlookInspector = Microsoft.Office.Interop.Outlook.Inspector;
   using OutlookMail = Microsoft.Office.Interop.Outlook.MailItem;
   class MailManager
   {
      public void StartUp(OutlookApplication application)
      {
         _inspectors = application.Inspectors;
         _inspectors.NewInspector += Inspectors_NewInspector;
      }

      private void Inspectors_NewInspector(OutlookInspector Inspector)
      {
         if (Inspector.CurrentItem is OutlookMail)
         {
            OutlookMail mail = (Inspector.CurrentItem as OutlookMail);
            mail.BeforeAttachmentAdd += Mail_BeforeAttachmentAdd;
         }
      }

      private void Mail_BeforeAttachmentAdd(OutlookAttachment Attachment, ref bool Cancel)
      {
         /*Never called without Visual Studio*/
      }
   }
}

Thanks for your help.

Upvotes: 1

Views: 169

Answers (2)

Dmitry Streblechenko
Dmitry Streblechenko

Reputation: 66235

The object firing the events (mail variable in your code) must be on the global/class level to prevent it from being garbage collected. The variable is local in you case.

On a general note, you can have multiple inspectors open, so it might make sense to have a wrapper object that holds references to the inspector and its mail item, and have a list of such wrappers in your addin.

Upvotes: 1

Benoit Patra
Benoit Patra

Reputation: 4545

From what I see you may be hitting a My button stopped working issue From the book Of E. Carter and E. Lippert VSTO 2007

It states

One issue commonly encountered when beginning to program agains Office events in .NET is known as the "my button stopped working" issue. A developer will write some code to handle a Click event raised by a CommandBarButton in the Office toolbar object model. This code will sometimes work temporarily but then stop. The user will click the button, but the Click event appears to have stopped working. The cause of this issue is connecting an event handler to an object whose lifetime does not match the desired lifetime of the event. This tipcally occurs when the object to which you are connecting an event handler goes out of scope or gets sets to null so that it gets garbage collected.

I think in your case that the .NET RCW object of type OulookMail manipulated with variable mail that is the culprit. Its lifetime is not properly handled. The fact that this does not happen in Visual Studio is that you are probably in Debug mode that changes a little the Garbage Collection so your object is not destroyed yet when you do the testing.

Upvotes: 0

Related Questions