Ribbon XML File

If you want more complicated functionality and greater control then you can edit the XML file directly.
This approach does require you to make some code changes to the ThisAddIn.cs file.
The "Ribbon.xml" file must have a Build Action of "Embedded Resource".


Insert New File

Project > Add New > Item > Ribbon (XML)
Change the filename "Ribbon" (instead of Ribbon1)
This will add 2 new files
Ribbon.xml
Ribbon.cs


ThisAddIn.cs

Your add-in must implement the Microsoft.Office.Core.IRibbonExtensibility interface.
Add the following variable and method to the ThisAddIn class.

public partial class ThisAddIn 
{
   public Microsoft.Office.Core.IRibbonUI _ribbonUI;

   protected override Microsoft.Office.Core.IRibbonExtensibility CreateRibbonExtensibilityObject()
   {
      return new Ribbon();
   }
}

Ribbon.xml

This file contains the XML tags.

<customUI xmlns="http://schemas.microsoft.com/office/2009/07/customui" onLoad="Ribbon_Load"> 
  <ribbon>
    <tabs>
      <tab id="CustomTab" label="My Tab">
        <group id="SimpleControls" label="My Group">
          <button id="Button1" imageMso="HappyFace" size="large"
            label="Large Button"
            onAction="Button1_Click"/>
        </group>
      </tab>
    </tabs>
  </ribbon>
</customUI>

Ribbon.cs

The GetCustomUI method is from the IRibbonExtensibility interface.
The GetCustomUI method can either get the XML from a customisation file or from code embedded in the add-in.
The GetCustomUI should do nothing else except return the XML markup, no initialisation, especially no prompts or dialog boxes.

public class Ribbon : Microsoft.Office.Core.IRibbonExtensibility 
{
   // remove this declaration line because it has been moved to the ThisAddIn class
   private Office.IRibbonUI ribbon;

   public Ribbon()
   {
   }

   public string GetCustomUI(string ribbonID)
   {
       return GetResourceText("ProjectName.Ribbon.xml");
   }

   public void Ribbon_Load(Office.IRibbonUI ribbonUI)
   {
      // modify this line to use ThisAddIn
      Globals.ThisAddIn._ribbonUI = ribbonUI;
   }

   private static string GetResourceText(string resourceName)
   {
       Assembly asm = Assembly.GetExecutingAssembly();
       string[] resourceNames = asm.GetManifestResourceNames();
       for (int i = 0; i < resourceNames.Length; ++i)
       {
           if (string.Compare(resourceName, resourceNames[i], StringComparison.OrdinalIgnoreCase) == 0)
           {
               using (StreamReader resourceReader =
                    new StreamReader(asm.GetManifestResourceStream(resourceNames[i])))
               {
                   if (resourceReader != null)
                   {
                       return resourceReader.ReadToEnd();
                   }
               }
           }
       }
       return null;
   }

   public void Button1_Click(Microsoft.Office.Core.IRibbonControl control)
   {
   }
}

© 2021 Better Solutions Limited. All Rights Reserved. © 2021 Better Solutions Limited TopPrevNext