Ribbon Interface

Custom Ribbon Interfaces (Excel 2007 and later).


MyRibbon.cs

Add a new file "MyRibbon.cs" to contain all the ribbon specific code.

public class MyRibbon : ExcelDna.Integration.CustomUI.ExcelRibbon 
{
    public void Ribbon_Loading(ExcelDna.Integration.CustomUI.IRibbonUI ribbon)
    {
        System.Windows.Forms.MessageBox.Show("Ribbon Loading");
    }

    public void Button_OnAction(ExcelDna.Integration.CustomUI.IRibbonControl control)
    {
        System.Windows.Forms.MessageBox.Show("Button_OnAction");
    }

    public int DropDown_OnGetItemCount(ExcelDna.Integration.CustomUI.IRibbonControl control)
    {
        System.Windows.Forms.MessageBox.Show("DropDown_OnGetItemCount");
        return 1;
    }

    public string DropDown_OnGetItemLabel(ExcelDna.Integration.CustomUI.IRibbonControl control, int Index)
    {
        System.Windows.Forms.MessageBox.Show("DropDown_OnGetItemLabel");
        return "value 1";
    }

    public int DropDown_OnGetSelectedItemIndex(ExcelDna.Integration.CustomUI.IRibbonControl control)
    {
        System.Windows.Forms.MessageBox.Show("DropDown_OnGetSelectedItemIndex");
        return 0;
    }
}

ExcelDNALibrary-AddIn.dna

Add the Ribbon XML to the configuration file.
Notice that the XML needs to be placed inside a "CustomUI" tag.

<?xml version="1.0" encoding="utf-8"?> 
<DnaLibrary >
<ExternalLibrary />

<CustomUI>
  <customUI xmlns="http://schemas.microsoft.com/office/2009/07/customui" onLoad="Ribbon_Loading">
  <ribbon startFromScratch="false">
    <tabs>
       <tab id="CustomTab" insertBeforeMso="TabHome" label="My Add-in">
       <group id="SimpleControls" label="My Group">
       <button id="Button1" imageMso="PictureCorrectionsMenu" size="large"
         label="Large Button"
         screentip="my long description"
         onAction="Button_OnAction"/>

       <dropDown id="DropDown1"
         getItemCount="DropDown_OnGetItemCount"
         getItemLabel="DropDown_OnGetItemLabel"
         getSelectedItemIndex="DropDown_OnGetSelectedItemIndex">
       </dropDown>
       </group>
       </tab>
    </tabs>
  </ribbon>
  </customUI>
</CustomUI>

</DnaLibrary>

MyRibbon.xml

Instead of adding the XML to the configuration file an alternative is to place it in a separate file.
Add a new file "MyRibbon.xml" to contain the ribbon specific code.
Make sure that the Build Action for this file is an Embedded Resource (this can be changed from the Properties window).

<?xml version="1.0" encoding="utf-8" ?> 
  <customUI xmlns="http://schemas.microsoft.com/office/2009/07/customui" onLoad="Ribbon_Loading">
  <ribbon startFromScratch="false">
    <tabs>
       <tab id="CustomTab" insertBeforeMso="TabHome" label="My Add-in">
       <group id="SimpleControls" label="My Group">
       <button id="Button1" imageMso="PictureCorrectionsMenu" size="large"
         label="Large Button"
         screentip="my long description"
         onAction="Button_OnAction"/>
       </group>
       </tab>
    </tabs>
  </ribbon>
  </customUI>

Add this additional "GetCustomUI" method to the "MyRibbon.cs" file.

public class MyRibbon : ExcelDna.Integration.CustomUI.ExcelRibbon 
{
   public override string GetCustomUI(string ribbonId)
   {
       string ribbonXml;
       var thisAssembly = typeof(MyRibbon).Assembly;
       var resourceName = typeof(MyRibbon).Namespace + ".MyRibbon.xml";

       using (Stream stream = thisAssembly.GetManifestResourceStream(resourceName))
       using (StreamReader reader = new StreamReader(stream))
       {
           ribbonXml = reader.ReadToEnd();
       }
       return ribbonXml;
   }

   public void Ribbon_Loading(ExcelDna.Integration.CustomUI.IRibbonUI ribbon)
   public void Button_OnAction(ExcelDna.Integration.CustomUI.IRibbonControl control)
}

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