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
which means I am injecting 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 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.
I also tried reflection which results in MissingMethodException
.
System.Type t = this.addIn.GetType();
var ctps = t.InvokeMember(
"get_CustomTaskPanes",
BindingFlags.InvokeMethod | BindingFlags.NonPublic,
null,
this.addIn,
null);
var ctps = t.InvokeMember(
"get_CustomTaskPanes",
BindingFlags.GetProperty| BindingFlags.NonPublic,
null,
this.addIn,
null);
var ctps = t.InvokeMember(
"CustomTaskPanes",
BindingFlags.GetProperty | BindingFlags.NonPublic,
null,
this.addIn,
null);
What else can I do to access the custom task panes collection of the AddIn from the shared library?
Have you seen this workaround here? http://davecra.wordpress.com/2013/05/21/customtaskpanes-in-a-net-shared-com-add-in/