Search code examples
vstooutlook-addinnetoffice

NetOffice - Custom Task Pane in a Appointment window in Outlook


I found out that it's possible to add custom task panes to individual windows like e.g. the appointment with this code snippet:

    public void Inspectors_NewInspector(Microsoft.Office.Interop.Outlook.Inspector Inspector)
{
    Microsoft.Office.Tools.CustomTaskPane myCustomTaskPane;

    if(Inspector.CurrentItem is  Microsoft.Office.Interop.Outlook.AppointmentItem ) {

        UserControl uc1 = MyUserControl();
        myCustomTaskPane = getAddIn().CustomTaskPanes.Add(uc1, "MyPanel",Inspector);
        myCustomTaskPane.DockPosition = Office.MsoCTPDockPosition.msoCTPDockPositionRight;
        myCustomTaskPane.DockPositionRestrict = Office.MsoCTPDockPositionRestrict.msoCTPDockPositionRestrictNoChange;
        myCustomTaskPane.Visible = true;

    }

    //Additionally You can add a property change listener to the current Item here
}

however, I use 'NetOffice' instead of VSTO to have the add-in compatible with various Outlook versions. And there the add-in doesn't have the CustomTaskPanes property, and the TaskPanes.Add property isn't overloaded to allow adding custom panes on other window than the main explorer.


Solution

  • Ok, worked it out in following way. In the ComAddin class I have a local variable

        Office._CustomTaskPane _taskPane;
    

    and I set the variable on the overriden CTPFactoryAvailable method:

        public override void CTPFactoryAvailable(object CTPFactoryInst)
        {
                _ctpFactory = new NetOffice.OfficeApi.ICTPFactory(this.Application, CTPFactoryInst);
        }
    

    Then - when the addin is loaded - I'm adding an event handler to the NewInspectorEvent event:

        private void Addin_OnStartupComplete(ref Array custom)
        {
            var inspectors = Application.Inspectors as NetOffice.OutlookApi.Inspectors;
            inspectors.NewInspectorEvent += Inspectors_NewInspectorEvent;
        }
    

    In the event handler for creating a new inspector window, I'm creating the pane:

        private void Inspectors_NewInspectorEvent(_Inspector Inspector)
        {
            var ai = Inspector.CurrentItem as AppointmentItem;
            if (ai == null)
                return;
            var ins = Inspector as NetOffice.OutlookApi.Inspector;
            _taskPane = _ctpFactory.CreateCTP(typeof(Addin).Assembly.GetName().Name + ".UserControl1", "My title", Inspector);
            _taskPane.DockPosition = MsoCTPDockPosition.msoCTPDockPositionTop;
            _taskPane.Height = 50;
            _taskPane.Visible = true;
        }
    

    This draft proof of concept works for me.