none
How to access the CustomTaskPanes property from a shared library? RRS feed

  • Question

  • I am building an AddIn for Word, Excel, PowerPoint. This AddIn comes with a ribbon. Whenever the user clicks a button on the ribbon a custom task pane is opened on the side. Since I do not want to rewrite the same code in three different projects I have a shared project where my ribbon is defined (XML).

    Problem: the ribbon callbacks have to access the CustomTaskPanes property.

    I have tried to inject the CustomTaskPanes property to the shared ribbon:

    public partial class ThisAddIn
    {
      protected override Office.IRibbonExtensibility CreateRibbonExtensibilityObject()
      {
        return new Shared.Ribbon(this.CustomTaskPanes);
      }
    
      ...
    }
    
    
    public class Ribbon : Office.IRibbonExtensibility
    {
      private Microsoft.Office.Tools.CustomTaskPaneCollection taskPanes;
    
      public Ribbon(Microsoft.Office.Tools.CustomTaskPaneCollection taskPanes)
      {
        this.taskPanes = taskPanes;
      }
    
      ...
    }
    

    But at the time when this method is executed the CustomTaskPanes property is null.

    Changing the constructor of the ribbon so that we can inject the AddIn to the ribbon does not work, too, since CustomTaskPanes property is internal the code throws an exception:

    public partial class ThisAddIn
    {
      protected override Office.IRibbonExtensibility CreateRibbonExtensibilityObject()
      {
        return new Shared.Ribbon(this);
      }
    
      ...
    }
    
    
    public class RecordsRibbon : Office.IRibbonExtensibility
    {
      private dynamic addIn;
    
      public RecordsRibbon(dynamic addIn)
      {
        this.addIn = addIn;
      }
    
      private void OpenTaskPane()
      {
        // RuntimeBinderException with message: 'ThisAddIn.CustomTaskPanes' is inaccessible due to its protection level
        var taskPane = this.addIn.CustomTaskPanes.Add(new System.Windows.Forms.UserControl(), "title");
        var taskPane.Width = 400;
        var taskPane.Visible = true;
      }
    
      ...
    }
    

    I do not want to change the access modifier of the ThisAddIn.CustomTaskPanes property since it is designer generated code.

    What else can I do to access the CustomTaskPanes collection from the shared library?

    Wednesday, January 22, 2014 4:36 PM

All replies