Ribbon XML File

Using the Ribbon Designer is fine for simple ribbons.
If you need 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"



Project > Add New > Item > Ribbon (XML)
Change the filename "customRibbon"
This add 2 new files
Ribbon1.cs
Ribbon1.xml
Delete all the comments




Ribbon1.cs

public class Ribbon1 : Microsoft.Office.Core.IRibbonExtensibility 
{
   private Office.IRibbonUI ribbon;

   public Ribbon1()
   {
   }

   // IRibbonExtensibility Member
   public string GetCustomUI(string ribbonID)
   {
   }

   public void Ribbon_Load(Office.IRibbonUI ribbonUI)
   {
      this.ribbon = 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;
   }
}

Your add-in must implement the Microsoft.Office.Core.IRibbonExtensibility interface
When an add-in (written in managed code) loads the application will call the QueryService method which determines if the add-in implements the IRibbonExtensibility interface.
If it does then the application will call the IRibbonExtensibility::GetCustomUI method which returns the XML markup to be loaded.
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.


Ribbon1.xml

ss - default



ThisAddIn.cs

Add the following method to the ThisAddIn class

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


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