none
Adding custom panes to Inspector windows (Outlook add-in) RRS feed

  • Question

  • UPDATE: This only appears to happen with the Email Inspector Window.

    I am able to add my custom control to several different inspector windows  using the CustomTaskPanes object. However, if each time I close and re-open the inspector window, I get a duplicate of my custom control.

    The way I'm currently adding my custom control is through the "NewInspector" event handler, so each time this gets called I'm adding my control to the CustomTaskPanes. Code snippet below:

    Object item = inspector.CurrentItem;
    if( item is Outlook.MailItem )
    {
    var customEmailTaskPane = this.CustomTaskPanes.Add(new EmailTaskPaneControl(), "Email Tasks", inspector);
    customEmailTaskPane.Visible = true;
    }

    This is inside my event handler. I suspect it's not disposing of my custom panel when the inspector gets closed but not sure how to handle this. How do I get rid of the duplicate panels? I know I can use a boolean instance variable which I can use to keep track of whether the custom panel has already been added, but it seems like there should be a better way.


    Wednesday, December 21, 2016 6:24 PM

Answers

  • Hello Jeremy,

    Looks like you need to add the event handler which frees resources when the e-mail message is closed. The event handler should also remove the current custom task pane from the CustomTaskPanes collection. This helps prevent multiple instances of the custom task pane when the next e-mail message is opened.

            void InspectorWrapper_Close()
            {
                if (taskPane != null)
                {
                    Globals.ThisAddIn.CustomTaskPanes.Remove(taskPane);
                }
    
                taskPane = null;
                Globals.ThisAddIn.InspectorWrappers.Remove(inspector);
                ((Outlook.InspectorEvents_Event)inspector).Close -=
                    new Outlook.InspectorEvents_CloseEventHandler(InspectorWrapper_Close);
                inspector = null;
            }

    See Walkthrough: Displaying Custom Task Panes with E-Mail Messages in Outlook for more information.


    profile for Eugene Astafiev at Stack Overflow, Q&A for professional and enthusiast programmers

    Wednesday, December 21, 2016 9:32 PM

All replies

  • Are you sure you don't process NewInspector event twice?

    Dmitry Streblechenko (MVP)
    http://www.dimastr.com/redemption
    Redemption - what the Outlook
    Object Model should have been
    Version 5.5 is now available!

    Wednesday, December 21, 2016 6:46 PM
  • Yes, I'm sure. The first time I open the inspector window for emails I only see one copy of my custom pane, but each time thereafter I see one more copy than there was the previous time I opened the inspector. So by the time I close and re-open the inspector the third time, I see three copies of my custom pane.

    The event handler seems to behave as expected, it gets called each time I open the inspector window, it just seems that the custom pane is not getting disposed with the rest of the inspector when I close it.


    Wednesday, December 21, 2016 6:52 PM
  • Hello Jeremy,

    Looks like you need to add the event handler which frees resources when the e-mail message is closed. The event handler should also remove the current custom task pane from the CustomTaskPanes collection. This helps prevent multiple instances of the custom task pane when the next e-mail message is opened.

            void InspectorWrapper_Close()
            {
                if (taskPane != null)
                {
                    Globals.ThisAddIn.CustomTaskPanes.Remove(taskPane);
                }
    
                taskPane = null;
                Globals.ThisAddIn.InspectorWrappers.Remove(inspector);
                ((Outlook.InspectorEvents_Event)inspector).Close -=
                    new Outlook.InspectorEvents_CloseEventHandler(InspectorWrapper_Close);
                inspector = null;
            }

    See Walkthrough: Displaying Custom Task Panes with E-Mail Messages in Outlook for more information.


    profile for Eugene Astafiev at Stack Overflow, Q&A for professional and enthusiast programmers

    Wednesday, December 21, 2016 9:32 PM
  • This seems like a reasonable solution, but I'm just not sure why the Email Inspector window needs this while other inspector windows don't.
    Thursday, December 22, 2016 5:06 PM
  • What other inspector windows are you talking about?

    profile for Eugene Astafiev at Stack Overflow, Q&A for professional and enthusiast programmers

    Thursday, December 22, 2016 5:28 PM
  • The one for Contacts as well as Appointments (calendar events). Neither of these exhibit the same behavior as the Emails Inspector (as in, they do not produce duplicate custom panels).
    Thursday, December 22, 2016 5:30 PM
  • Hi,

    If we handle the Application.Inspectors.NewInspector event using the following code, the custom task pane would be added repeatedly every time the event fires.

     

         private void ThisAddIn_Startup(object sender, System.EventArgs e)
            {
                Application.Inspectors.NewInspector += Inspectors_NewInspector;
            }
     
            private void Inspectors_NewInspector(Outlook.Inspector Inspector)
            {
                Object item = Inspector.CurrentItem;
                CustomTaskPane myCust = this.CustomTaskPanes.Add(new TaskPaneControl(), "test");
                myCust.Visible = true;
            }

     

    >>Neither of these exhibit the same behavior as the Emails Inspector (as in, they do not produce duplicate custom panels).

     

    If I use code above, all the inspectors perform the same behavior. Every time I create a new Email/Appointment/Contact, I get a new custom pane.

    I would suggest you disable all other add-ins and build a new one to test only the code above.

     

    I think the link Eugene shared shows a perfect method how to add a custom take pane to Inspector windows.

    I suggest you mark helpful post as answer if your issue has been resolved. Thanks for your understanding.

    Regards,

    Celeste


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    Wednesday, December 28, 2016 6:05 AM
    Moderator
  • Hi Celeste,

    Thank you for your input. However, my experience has been different in that for other items (anything other than "MailItem"), additional panels do not get created. Here's my event handler:

     
       private void Inspectors_NewInspector( Outlook.Inspector inspector )
    		{
    			Object item = inspector.CurrentItem;
    			if(item is Outlook.MailItem )
    			{
    				//var emailControl = new EmailTaskPaneControl();
    				var customEmailTaskPane = this.CustomTaskPanes.Add(new EmailTaskPaneControl(), "Email Tasks", inspector);
    				customEmailTaskPane.Visible = true;
    			}
    			else if(item is Outlook.ContactItem)
    			{
    				//var contactControl = new ContactTaskPaneControl();
    				var customContactTaskPane = this.CustomTaskPanes.Add(new ContactTaskPaneControl(), "Contact Pane", inspector);
    				customContactTaskPane.Visible = true;
    			}
    			else if (item is Outlook.AppointmentItem)
    			{
    				//var appointmentControl = new AppointmentTaskPaneControl();
    				var customContactTaskPane = this.CustomTaskPanes.Add( new AppointmentTaskPaneControl(), "Appointment Pane", inspector );
    				customContactTaskPane.Visible = true;
    			}
    		}


    I'm not doing anything different with each inspector yet the Email Inspector is the only one producing duplicate panels.

    Wednesday, December 28, 2016 9:23 PM
  • HI,

    Please visit Custom Task Panes:

    Preventing Multiple Instances of a Custom Task Pane in Outlook:

    To prevent Outlook windows from displaying multiple instances of a custom task pane, explicitly remove the custom task pane from the CustomTaskPanes collection of the ThisAddIn class when each window is closed. Call the Remove method in an event that is raised when a window is closed, such as ExplorerEvents_10_Event.Close or InspectorEvents_10_Event.Close.

    If you do not explicitly remove the custom task pane, Outlook windows might display multiple instances of the custom task pane. Outlook sometimes recycles windows, and recycled windows retain references to any custom task panes that were attached to them.

     

    So if you use window Parameter, we need to remove the custom task pane in an event that is raised when a window is closed.

    Regards,

    Celeste


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    Tuesday, January 3, 2017 9:21 AM
    Moderator