8

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?

Mark Lalor
  • 7,820
  • 18
  • 67
  • 106
  • AFAIK the designer creates XML behind the scene... so that should be possible - perhaps you could show some of the XML... – Yahia Aug 21 '11 at 00:42
  • Where does it make XML? Looks more like objects, like windows forms. – Mark Lalor Aug 21 '11 at 00:51
  • perhaps I misunderstood... which designer are you using ? – Yahia Aug 21 '11 at 00:53
  • Visual Ribbon Designer in Visual Studio 2010 for a Word 2007 Add-In – Mark Lalor Aug 21 '11 at 00:54
  • you can right-click in the designer and tell it to export the Ribbon to XML... – Yahia Aug 21 '11 at 00:58
  • Oh, yes I tried that but 1: You cant re-edit it. 2: It seems to lose stuff like the event handlers. 3: I want to know if this can be done without XML, like going into the designer code and modifying something. – Mark Lalor Aug 21 '11 at 01:01
  • not that I know of since there is no property/method... to manipulate xml-namespace etc. which is necessary for the "trick" shown in the video... – Yahia Aug 21 '11 at 01:05
  • Hmmmm, its seems odd that Microsoft wouldn't incorporate that into the visual. I think I'll try to get a new project running with that and see if I can make it work but.... there's just gotta be a way I feel it in my heart..... well.. maybe not but I hope so – Mark Lalor Aug 21 '11 at 01:08
  • wish you luck... I think the visual designer makes the assumption that there 1 AddIn per Ribbon... 1 AddIn can have multiple buttons etc. - for making it work with more than 1 AddIn you would need a project containing more than 1 AddIn... – Yahia Aug 21 '11 at 01:10
  • Unless in about a week someone comments saying they want to keep this up, I'm going to delete it because I don't think its possible and the XML was meant to be more powerful than the Visual Designer – Mark Lalor Aug 24 '11 at 18:46
  • It's more than a week, but I have a question, as I just discovered this..How do I determine which add-in's buttons appear where? The buttons that load at the top of my ribbon should be at the bottom, and vice-versa. (If this needs to be moved to a new question, just let me know). – Larry G. Wapnitsky Sep 15 '11 at 19:31
  • @larry-g-wapnitsky Its a good question, using an xml namespace I think there and attribute to do something like that and I saw a video that can help you with that I'm just having trouble finding it right now.. – Mark Lalor Sep 17 '11 at 20:55

1 Answers1

5

http://blogs.msdn.com/b/vsto/archive/2008/03/10/share-a-ribbon-customization-between-office-applications.aspx

Office 2007

Create the Ribbon

  1. 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.
  2. Add a Ribbon (Visual Designer) item to the project. For the purpose of these steps, accept the default name “Ribbon1”.
  3. Save and close the project.

Create a Class Library Project

  1. Create a new class library project in Visual Studio. For the purpose of these steps, name the project SharedRibbonLibrary.
  2. Add a project reference to the Microsoft.Office.Tools.Common.v9.0 assembly.
  3. On the Project Menu in Visual Studio, click Add Existing Item.
  4. 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.
  5. Double-click Ribbon1.cs. The Ribbon designer appears.
  6. From the Office Ribbon Controls tab of the Toolbox, drag a button onto group1.
  7. Click button1 to select it.
  8. 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.
  9. Right-click the Ribbon designer, and then click Properties.
  10. 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.
  11. In Solution Explorer, right-click Ribbon1.cs, and then click View Code.
  12. Change the namespace of the class to “SharedRibbonLibrary”.
  13. Repeat this step for the Ribbon1.designer.cs file.
  14. 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

  1. Create 2007 Excel, Outlook, PowerPoint, or Word project.
  2. Add a reference to the SharedRibbonLibrary assembly.
  3. 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() });
    
    }
    
  4. Add a new class to the project. Accept the default name “Class1.cs”.

  5. 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

  1. 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);
    
  2. 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!");
    }
    
  3. Run the project.

  4. 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/

Jeremy Thompson
  • 61,933
  • 36
  • 195
  • 321