Export (0) Print
Expand All

How to: Display Custom Task Panes with E-Mail Messages in Outlook

The following code examples display a unique custom task pane in the Inspector window of every e-mail message that is opened in Outlook. For more information, including instructions for using the code examples in this topic, see Walkthrough: Displaying Custom Task Panes with E-Mail Messages in Outlook.

Applies to: The information in this topic applies to application-level projects for Outlook 2007 and Outlook 2010. For more information, see Features Available by Office Application and Project Type.

ThisAddIn code file

  • Replace the contents of the ThisAddIn.cs or ThisAddIn.vb file in your add-in with the following code.

    
    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using Outlook = Microsoft.Office.Interop.Outlook;
    using Office = Microsoft.Office.Core;
    using Microsoft.Office.Tools;
    
    namespace OutlookMailItemTaskPane
    {
        public class InspectorWrapper
        {
            private Outlook.Inspector inspector;
            private CustomTaskPane taskPane;
    
            public InspectorWrapper(Outlook.Inspector Inspector)
            {
                inspector = Inspector;
                ((Outlook.InspectorEvents_Event)inspector).Close +=
                    new Outlook.InspectorEvents_CloseEventHandler(InspectorWrapper_Close);
    
                taskPane = Globals.ThisAddIn.CustomTaskPanes.Add(
                    new TaskPaneControl(), "My task pane", inspector);
                taskPane.VisibleChanged += new EventHandler(TaskPane_VisibleChanged);
            }
    
            void TaskPane_VisibleChanged(object sender, EventArgs e)
            {
                Globals.Ribbons[inspector].ManageTaskPaneRibbon.toggleButton1.Checked = 
                    taskPane.Visible;
            }
    
            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;
            }
    
            public CustomTaskPane CustomTaskPane
            {
                get
                {
                    return taskPane;
                }
            }
        }
    
        public partial class ThisAddIn
        {
            private Dictionary<Outlook.Inspector, InspectorWrapper> inspectorWrappersValue =
                new Dictionary<Outlook.Inspector, InspectorWrapper>();
            private Outlook.Inspectors inspectors;
    
            private void ThisAddIn_Startup(object sender, System.EventArgs e)
            {
                inspectors = this.Application.Inspectors;
                inspectors.NewInspector +=
                    new Outlook.InspectorsEvents_NewInspectorEventHandler(
                    Inspectors_NewInspector);
    
                foreach (Outlook.Inspector inspector in inspectors)
                {
                    Inspectors_NewInspector(inspector);
                }
            }
    
            private void ThisAddIn_Shutdown(object sender, System.EventArgs e)
            {
                inspectors.NewInspector -=
                    new Outlook.InspectorsEvents_NewInspectorEventHandler(
                    Inspectors_NewInspector);
                inspectors = null;
                inspectorWrappersValue = null;
            }
    
            void Inspectors_NewInspector(Outlook.Inspector Inspector)
            {
                if (Inspector.CurrentItem is Outlook.MailItem)
                {
                    inspectorWrappersValue.Add(Inspector, new InspectorWrapper(Inspector));
                }
            }
    
            public Dictionary<Outlook.Inspector, InspectorWrapper> InspectorWrappers
            {
                get
                {
                    return inspectorWrappersValue;
                }
            }
    
            #region VSTO generated code
    
            /// <summary>
            /// Required method for Designer support - do not modify
            /// the contents of this method with the code editor.
            /// </summary>
            private void InternalStartup()
            {
                this.Startup += new System.EventHandler(ThisAddIn_Startup);
                this.Shutdown += new System.EventHandler(ThisAddIn_Shutdown);
            }
    
            #endregion
        }
    }
    
    
    

ManageTaskPaneRibbon code file

  • Replace the contents of the ManageTaskPaneRibbon.cs or ManageTaskPaneRibbon.vb file in your add-in with the following code.

    
    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using Microsoft.Office.Tools.Ribbon;
    using Outlook = Microsoft.Office.Interop.Outlook;
    using Microsoft.Office.Tools;
    
    namespace OutlookMailItemTaskPane
    {
        public partial class ManageTaskPaneRibbon : RibbonBase
        {
            public ManageTaskPaneRibbon()
                : base(Globals.Factory.GetRibbonFactory())
            {
                InitializeComponent();
            }
    
            private void ManageTaskPaneRibbon_Load(object sender, RibbonUIEventArgs e)
            {
    
            }
    
            private void toggleButton1_Click(object sender, RibbonControlEventArgs e)
            {
                Outlook.Inspector inspector = (Outlook.Inspector)e.Control.Context;
                InspectorWrapper inspectorWrapper = Globals.ThisAddIn.InspectorWrappers[inspector];
                CustomTaskPane taskPane = inspectorWrapper.CustomTaskPane;
                if (taskPane != null)
                {
                    taskPane.Visible = ((RibbonToggleButton)sender).Checked;
                }
            }
        }
    }
    
    
    

Community Additions

ADD
Show:
© 2014 Microsoft