Question posted 2011 · +7 upvotes
I’m trying to make two word add-ins’ groups to appear in the same tab (Tools) but they both create unique tabs (there’s two ‘Tools’ tabs). I saw this video but I’m using the Visual Designer, not XML.
Can I edit the designer code in some way to make this work?
Accepted answer +4 upvotes
Office 2007
Create the Ribbon
- Create a 2007 Excel, Outlook, PowerPoint, or Word project in Visual Studio. For the purpose of these steps, create a C# project and name the project RibbonStarterProject.
- Add a Ribbon (Visual Designer) item to the project. For the purpose of these steps, accept the default name âRibbon1â.
- Save and close the project.
Create a Class Library Project
- Create a new class library project in Visual Studio. For the purpose of these steps, name the project SharedRibbonLibrary.
- Add a project reference to the Microsoft.Office.Tools.Common.v9.0 assembly.
- On the Project Menu in Visual Studio, click Add Existing Item.
- In the Add Existing Item dialog box, browse to the âRibbonStarterProjectâ project directory, select the Ribbon.cs file, and click Add. Ribbon1.cs is copied to the project directory and appears beneath the project node in Solution Explorer.
- Double-click Ribbon1.cs. The Ribbon designer appears.
- From the Office Ribbon Controls tab of the Toolbox, drag a button onto group1.
- Click button1 to select it.
- In the Properties window, set Modifiers to Public. Note: By default, controls that you add to the Ribbon are Internal. That makes them only accessible to code inside the same assembly. However, when you access these controls, you will be accessing them through an assembly reference. Therefore, to reach them from code, you must make them public. More on this soon.
- Right-click the Ribbon designer, and then click Properties.
- In the Properties window, click the RibbonType property, and then select the Ribbon IDâs of the applications or Outlook Inspector windows in which you want the Ribbon to appear. For more information about this property, see the MSDN reference topic for the RibbonType property.
- In Solution Explorer, right-click Ribbon1.cs, and then click View Code.
- Change the namespace of the class to âSharedRibbonLibraryâ.
- Repeat this step for the Ribbon1.designer.cs file.
- Compile and save the SharedRibbonLibrary project. You can now use the resulting assembly in any VSTO project that supports the Ribbon.
Consume the Ribbon Customization
- Create 2007 Excel, Outlook, PowerPoint, or Word project.
- Add a reference to the SharedRibbonLibrary assembly.
Add the following code to the ThisAddin, ThisWorkbook, or ThisDocument class of your project. This code overrides the CreateRibbonExtensibilityObject method and returns the Ribbon to the Office application.
protected override Microsoft.Office.Core.IRibbonExtensibility CreateRibbonExtensibilityObject() { return new Microsoft.Office.Tools.Ribbon.RibbonManager( new Microsoft.Office.Tools.Ribbon.OfficeRibbon[] { new SharedRibbonLibrary.Ribbon1() }); }Add a new class to the project. Accept the default name âClass1.csâ.
Replace the code in the Class1 file with the following:
partial class ThisRibbonCollection : Microsoft.Office.Tools.Ribbon.RibbonReadOnlyCollection { internal SharedRibbonLibrary.Ribbon1 Ribbon1 { get { return this.GetRibbon<SharedRibbonLibrary.Ribbon1>(); } } }
Ok â You are done! You can now access the Ribbon and the button that you added to the Ribbon in your code. Lets try by handling an event in the consuming project.
Handle the Button Click Event
Add the following code to the startup event handler of project.
Globals.Ribbons.Ribbon1.button1.Click += new EventHandler<Microsoft.Office.Tools.Ribbon.RibbonControlEventArgs>(button1_Click);Add the following event handler to your project:
void button1_Click(object sender, Microsoft.Office.Tools.Ribbon.RibbonControlEventArgs e) { System.Windows.Forms.MessageBox.Show("I can handle events!"); }Run the project.
When the Office application opens, click the Add-Ins tab, and then click your button. A message that says âI can handle events!â appears.
Office 2010 implementation: http://blogs.msdn.com/b/vsto/archive/2010/06/23/sharing-a-ribbon-customization-between-office-projects-in-visual-studio-2010-mclean-schofield.aspx
The 2010 implementation actually add’s two Ribbons – one for each Add-In. I believe the article is only applicable to Add-Ins on the same Ribbon in different Office products (eg Word and Excel) not two Excel Add-ins.
The only other avenue I’ve found is a 3rd party component: http://www.add-in-express.com/creating-addins-blog/2012/11/05/excel-addin-shared-ribbon-tabs/
4 code variants in this answer
- Variant 1 — 8 lines, starts with
protected override Microsoft.Office.Core.IRibbonExtensibili… - Variant 2 — 7 lines, starts with
partial class ThisRibbonCollection : Microsoft.Office.To… - Variant 3 — 1 lines, starts with
Globals.Ribbons.Ribbon1.button1.Click += new EventHandler<M… - Variant 4 — 5 lines, starts with
void button1_Click(object sender,
External references cited (2)
- blogs.msdn.com — http://blogs.msdn.com/b/vsto/archive/2008/03/10/share-a-ribbon-customization-between-office-applications.aspx
- add-in-express.com — http://www.add-in-express.com/creating-addins-blog/2012/11/05/excel-addin-shared-ribbon-tabs/
VBA Core objects referenced (4)
Microsoft.Office— Controlling One Microsoft Office Application from AnotherMicrosoft.Office— List the Name and Office Location of Each Manager Belonging to an Exchange Distribution ListTools.Common— Automating Common Word TasksTools.Ribbon— Add a Command to the Ribbon to Start a Reply Form
Top ms-office Q&A (6)
- Detect whether Office is 32bit or 64bit via the registry +45 (2010)
- Installing Office Customization +31 (2009)
- What does the 'x' in the extensions aspx, docx, xlsx, etc. represent? +11 (2010)
- MSOFFICE MIME type verification +10 (2012)
- Problems with office automation in asp.net. I can use alternatives such as open-office, if I knew how +10 (2010)
- Can I embed an exe payload in a pdf, doc, ppt or any other file format? +10 (2010)
ms-office solutions on this site
.