Develop Office Business Applications using Visual Studio
A customer on the VSTO forums recently noticed that the steps we blogged about for sharing a single Ribbon customization between multiple projects in Visual Studio 2008 no longer works in Visual Studio 2010. When following the instructions in the blog post, you get an error when you try to open the Ribbon code file in the class library project. This is because of some changes to the way the Ribbon designer was implemented in Visual Studio 2010. The upshot is that you can no longer open the Ribbon designer outside of Office projects. In addition, because Office projects that target the .NET Framework 4 have a different programming model in Visual Studio 2010, the instructions in the old blog post will also result in other errors in projects that target the .NET Framework 4.
This blog post tells you how to share a single Ribbon customization (created by using the Ribbon designer) between multiple Office projects in Visual Studio 2010. Disclaimer – what I am about to show you in this blog post is not supported, and has not been officially tested by the product team in any way.
If your Office projects target the .NET Framework 3.5, then the instructions in the old blog post still apply, with just one change. Rather than design your Ribbon customization in the class library project, now you must design it in an Office project, because of the change to the implementation of the Ribbon designer noted above. Here are the modified instructions.
protected override Microsoft.Office.Core.IRibbonExtensibility CreateRibbonExtensibilityObject(){ return new Microsoft.Office.Tools.Ribbon.RibbonManager( new Microsoft.Office.Tools.Ribbon.OfficeRibbon[] { new SharedRibbonLibrary.Ribbon1() });}
partial class ThisRibbonCollection : Microsoft.Office.Tools.Ribbon.RibbonReadOnlyCollection{ internal SharedRibbonLibrary.Ribbon1 Ribbon1 { get { return this.GetRibbon<SharedRibbonLibrary.Ribbon1>(); } }}
private void ThisAddIn_Startup(object sender, System.EventArgs e){ Globals.Ribbons.Ribbon1.button1.Click += new EventHandler<Microsoft.Office.Tools.Ribbon.RibbonControlEventArgs>(button1_Click);}void button1_Click(object sender, Microsoft.Office.Tools.Ribbon.RibbonControlEventArgs e){ System.Windows.Forms.MessageBox.Show("it works!");}
If your Office projects target the .NET Framework 4, you’ll need to make a number of other changes to the Ribbon code. Here are the full instructions.
public Ribbon1(Microsoft.Office.Tools.Ribbon.RibbonFactory factory) : base(factory){ InitializeComponent();}
protected override Microsoft.Office.Tools.Ribbon.IRibbonExtension[] CreateRibbonObjects(){ return new Microsoft.Office.Tools.Ribbon.IRibbonExtension[] { new SharedRibbonLibrary.Ribbon1(Globals.Factory.GetRibbonFactory()) };}
private void ThisAddIn_Startup(object sender, System.EventArgs e){ Globals.Ribbons.Ribbon1.button1.Click += new Microsoft.Office.Tools.Ribbon.RibbonControlEventHandler(button1_Click);}void button1_Click(object sender, Microsoft.Office.Tools.Ribbon.RibbonControlEventArgs e){ System.Windows.Forms.MessageBox.Show("it works!");}
I have this working quite well in .NET4 in Excel/PPT/Word. I coupled your code with the Outlook Inspector wrapper (msdn.microsoft.com/.../ff973716.aspx), and get errors on the second email inspector instance (first can be open or closed). The first email instance seems to be ok. The error is long...something like:
An exception occurred while calling function "GetVisible". The exceptino message is:
Thrown exception of typ System.MissingMethodException with message" "No parameterless constructor defined for this object."
Stack trace.....
{stuff}
at Microsoft.Office.Tools.Ribbon.RibbonManagerImpl.System.Reflection.IReflect.InvokeMember( ---stuff --)