Skip to main content

Customizing Ribbons

Setting Ribbon Properties

The Ribbon extensibility model is a bit unusual. There is no opportunity to set the 'label' or 'image' property of the button after it is created, but there are getLabel and getImage callbacks that you can set up.

To get Excel to refresh your control (or the whole Ribbon extension) you need to set an onLoad callback (on the customUI element) which receives an IRibbonUI interface for you to keep. This interface has two methods - Invalidate and InvalidateControl - which you call when a control should be refreshed.

Excel-DNA can help with the implementation of the getImage callback - call the ExcelRibbon.LoadImage method (probably as base.LoadImage(imageId) in your code) with the imageId of the picture you want to show - this way you can load the images you specify in the .dna file.

To create dynamically set up ribbons, you need to build the xml also in a callback method (GetCustomUI). The RibbonVB Example in the Samples repository shows a project that handles dynamic ribbon elements (also handling menu visibility, dynamic screentip and image display, dropdowns and recursive menus) as well as context menus (cell, row, column etc.), utilization of an inbuilt commandbar and handling of VBE.Interop Command buttons.

Another VB Example is in the tutorials: RibbonBasics, a Csharp Example can be found in Ribbon.

If you look for the imageMso identifiers for the built-in ribbon controls, these can easily be looked up when customizing the ribbon (context menu "customize menu" in the ribbon) and hovering over the control of interest (the imageMso is shown in brackets after the control name).

To create dynamically set up ribbons, you need to build the xml also in a callback method (GetCustomUI). The below example shows a class that handles dynamic ribbon elements (also handling menu visibility, dynamic screentip and image display, dropdowns and recursive menus) as well as context menus (cell, row, column etc.), utilization of an inbuilt commandbar. To create dynamically set up ribbons, you need to build the xml also in a callback method (GetCustomUI). The RibbonVB Example in the Samples repository shows a project that handles dynamic ribbon elements (also handling menu visibility, dynamic screentip and image display, dropdowns and recursive menus) as well as context menus (cell, row, column etc.), utilization of an inbuilt commandbar and handling of VBE.Interop Command buttons.

Another VB Example is in the tutorials: RibbonBasics, a Csharp Example can be found in Ribbon.

If you look for the imageMso identifiers for the built-in ribbon controls, these can easily be looked up when customizing the ribbon (context menu "customize menu" in the ribbon) and hovering over the control of interest (the imageMso is shown in brackets after the control name).

References

Articles on MSDN:

Some Excel 2010 info:

Also note that a ribbon designed in VSTO can be exported to xml, which gives a <customUI ...> tag that can be used directly in Excel-DNA, though the ribbon handlers have to be re-implemented.