none
Event not fires after deployed to Outlook 2013 RRS feed

  • Question

  • private void ThisAddIn_Startup(object sender, System.EventArgs e)
    {....

    explorer.SelectionChange += new Microsoft.Office.Interop.Outlook.ExplorerEvents_10_SelectionChangeEventHandler(Explorer_Event);

    ....

    }

    public void Explorer_Event()
    {

            if (this.Application.ActiveExplorer().Selection.Count > 0)
            {

                        Object selObject = this.Application.ActiveExplorer().Selection[1];
                        if (selObject is Outlook.MailItem)
                        {
                              Outlook.MailItem mailItem =
                               (selObject as Outlook.MailItem);

                              ((Outlook.ItemEvents_10_Event)selObject).Reply += new Microsoft.Office.Interop.Outlook.ItemEvents_10_ReplyEventHandler(MailItem_Reply);

    ......

    }

     public void MailItem_Reply(Object response, ref bool cancel)
     {**************could not fire in this function, it can fire when running as debug in VS2012***************

    }

    What is the problem that function is not fires after deployed?



    • Edited by _dson Monday, March 14, 2016 7:31 AM
    Monday, March 14, 2016 7:28 AM

Answers

  • Hi _dson,

    I suppose you get explorer this way,

    explorer = this.Application.ActiveExplorer();

    In order for this Reply event to be fired, you'll need to not only declare selObject as a global variable, but also the "explorer".

    Keep in mind that you only have one global instance of selObject/explorer, it only keeps one reference of the object. The other ones that're out of the scope will be GC, meaning the Reply event won't fire.

    Also, it might be a good practice to "unsubscribe" the event before register it, otherwise the event handler might be registered/invoked multiple times.

    ((Outlook.ItemEvents_10_Event)selObject).Reply -= new Outlook.ItemEvents_10_ReplyEventHandler(MailItem_Reply);
    ((Outlook.ItemEvents_10_Event)selObject).Reply += new Outlook.ItemEvents_10_ReplyEventHandler(MailItem_Reply);



    • Edited by Jackie_ Tuesday, March 15, 2016 8:21 AM
    • Marked as answer by _dson Wednesday, March 16, 2016 1:31 AM
    Tuesday, March 15, 2016 5:12 AM

All replies

  • Hello,

    The source object should be alive all the time you want to handle events. You need to declare it at the global scope to prevent the object from swiping by the garbage collector. For example:

     ((Outlook.ItemEvents_10_Event)selObject).Reply += new Microsoft.Office.Interop.Outlook.ItemEvents_10_ReplyEventHandler(MailItem_Reply);

    As soon as the selObject goes out of scope and the GC runs you will not get any events fired.

    Moreover, I'd recommend releasing underlying COM objects instantly. Use System.Runtime.InteropServices.Marshal.ReleaseComObject to release an Outlook object when you have finished using it. This is particularly important if your add-in attempts to enumerate more than 256 Outlook items in a collection that is stored on a Microsoft Exchange Server. If you do not release these objects in a timely manner, you can reach the limit imposed by Exchange on the maximum number of items opened at any one time. Then set a variable to Nothing in Visual Basic (null in C#) to release the reference to the object. Read more about that in the Systematically Releasing Objects article.

    Also you may find the following articles helpful:

    How to: Implement a Wrapper for Inspectors and Track Item-Level Events in Each Inspector

    Developing an Inspector Wrapper for Outlook 2010

    Outlook 2010: Developing an Inspector Wrapper

    Monday, March 14, 2016 1:24 PM
  • Thank you for your reply.

    How to set Release Object in ThisAddIn_Shutdown?

    And I want to get the event when the email in view panel, not open as Inspector. What is the best way to do?

    Many thanks!

    Tuesday, March 15, 2016 1:30 AM
  • I tried to put the selObject in class ThisAddIn, but still cannot fires...
    Tuesday, March 15, 2016 1:54 AM
  • Hi _dson,

    I suppose you get explorer this way,

    explorer = this.Application.ActiveExplorer();

    In order for this Reply event to be fired, you'll need to not only declare selObject as a global variable, but also the "explorer".

    Keep in mind that you only have one global instance of selObject/explorer, it only keeps one reference of the object. The other ones that're out of the scope will be GC, meaning the Reply event won't fire.

    Also, it might be a good practice to "unsubscribe" the event before register it, otherwise the event handler might be registered/invoked multiple times.

    ((Outlook.ItemEvents_10_Event)selObject).Reply -= new Outlook.ItemEvents_10_ReplyEventHandler(MailItem_Reply);
    ((Outlook.ItemEvents_10_Event)selObject).Reply += new Outlook.ItemEvents_10_ReplyEventHandler(MailItem_Reply);



    • Edited by Jackie_ Tuesday, March 15, 2016 8:21 AM
    • Marked as answer by _dson Wednesday, March 16, 2016 1:31 AM
    Tuesday, March 15, 2016 5:12 AM