Pieter van der Westhuizen

Customizing built-in Office Ribbon groups – C# Excel add-in example

Customizing the built-in Microsoft Office ribbon groups is something that many people have tried but they usually hit a brick wall in the process. Unfortunately, it is not possible to customize the built-in ribbon groups, however, you can create your custom Office ribbon group and “replace” the built-in group with your own. The article explains how to do this on an example of a C# Excel add-in:

Finding the ribbon group ID

Before you can replace any built-in control or group, you first need to determine its internal id. You can download a list of these control ids from Microsoft:

Another way to find the id of a built-in Office control is to look at the tooltip when hovering over the corresponding entry on the Excel Option dialog’s Customize Ribbon tab:

Finding the id of a built-in Office control using the Customize Ribbon tab

Customizing the Excel Illustrations group

Let’s take a look at how you can add you own Ribbon button to the built-in Illustrations group on the Excel Insert tab.

The Illustrations group on the Excel Ribbon

First, we need to establish its internal id by looking at the list of built-in id’s we’ve downloaded earlier. In this case the Illustrations group’s id is GroupInsertIllustrations and the Tables group, GroupInsertTablesExcel. It is important to know the Tables groups’ id, because we’ll need to specify the id of the built-in group we want our custom group to be shown after.

Next, add a new Ribbon Tab control to your Add-in Express add-in projects’ AddinModule and add a new Ribbon group to it. Set the new Ribbon groups’ properties to:

  • Name : illustrationsRibbonGroup
  • Caption: Illustrations(Hidden)
  • IdMso: GroupInsertIllustrations
  • Ribbons: ExcelWorkbook
  • Visible: False

Setting the IdMso to the built-in groups’ id and setting the Visible property to False, will hide the built-in Excel Ribbon group.

Next, add a new Ribbon group to the Ribbon Tab control and set its properties as follows:

  • Name: customIllustrationsRibbonGroup
  • Capion: Illustrations
  • InsertAfterIdMso: GroupInsertTablesExcel

Add a Ribbon button, to replace the built-in Pictures button, to the group with the following properties:

  • Name: insertPicturesRibbonButton
  • Caption: Pictures
  • IdMso: PictureInsertFromFile
  • ImageMso: PictureInsertFromFile
  • Size: Large

Add another button for Online Pictures:

  • Name: insertOnlinePicturesRibbonButton
  • Caption: Online Pictures
  • IdMso: ClipArtInsertDialog
  • ImageMso: ClipArtInsertDialog
  • Size: Large

Add a Ribbon Gallery control to replace the built-in Shapes control:

  • Name: insertShapesRibbonGallery
  • Caption: Shapes
  • IdMso: ShapesInsertGallery
  • ImageMso: ShapesInsertGallery
  • Size: Large

Add one more button to replace the SmartArt button:

  • Name: insertSmartArtRibbonButton
  • Caption: SmartArt
  • IdMso: SmartArtInsert
  • ImageMso: SmartArtInsert
  • Size: Large

Finally, add one more Ribbon gallery control for Screenshot:

  • Name: insertScreenshotRibbonGallery
  • Caption: Screenshot
  • IdMso: ScreenshotInsertGallery
  • ImageMso: ScreenshotInsertGallery
  • Size: Large

Setting the ImageMso property to the id of the control will set its icon to the default built-in icon. We’ll include one more button that will add our own functionality to the built-in Excel Illustrations group. Add a custom button, and set its properties to the following:

  • Name: insertFromLibraryRibbonButton
  • Caption: From Library
  • ScreenTip: Insert Image from Library
  • SuperTip: Insert an image from the local image library stored on your server.
  • Size: Large

The final design of our custom Excel Ribbon tab should look similar to the following image:

The final design of our custom Excel Ribbon tab

Build, register and run your project and you’ll notice that the Illustrations group is still visible in Excel and all the default functionality still exist i.e. clicking the Pictures button still shows a dialog picker with which you can choose which picture to insert.

You’ll also see our own custom “From Library” button in the group. When you are hovering the mouse cursor over the button, you’ll notice it shows a special Screen Tip that also displays the name of your add-in.

A custom button added to the Illustration group in Excel 2013

This is a default design for any Microsoft Office application and serves as a way to identify non-built-in controls. Unfortunately, there is no way to disable this.

Replacing a built-in Office control

I’ve shown you how to add your custom control to a built-in ribbon group, but what if you would like to replace the functionality of one of the built-in controls? For example, if you want to show the user a custom form for inserting pictures into Excel when they click on the Pictures button in the Illustrations group.

To do this, select the Pictures button (insertPicturesRibbonButton) we’ve added earlier and clear its IdMso property. Set the ImageMso property value to PictureInsertFromFile. Because we’re replacing a built-in control with our own, we need to set both the ScreenTip and SuperTip properties. Note that some controls may require you to manually handle keyboard shortcuts if replaced.

Next, double-click next to the buttons’ OnClick property in the properties window.

Generating an event handler for the button's OnClick event

This will generate an event handler for the button’s OnClick event, you can add the logic to display a custom UI in here.

private void insertPicturesRibbonButton_OnClick(object sender,
    IRibbonControl control, bool pressed)
{
    MessageBox.Show("Code to show custom UI goes here");
}

Version neutrality caveats

Something you’ll need to keep in mind when customizing Ribbons for multiple versions of Office, is that the Ribbon control ids can vary between the different versions. It is also possible that the control does not exist in all versions of Office.

For example, if you consider the following screenshots of the Illustrations group in Excel 2007, 2010 and 2013, you’ll notice that Excel 2010 and 2013 has a Screenshot gallery button, but it is missing from Excel 2007.

Excel 2007:

The Illustrations group in Excel 2007

Excel 2010:

The Illustrations group in Excel 2010

Excel 2013:

The Illustrations group in Excel 2013

The first 4 Illustrations group’s controls in Excel 2007 and 2010 are exactly the same and share the same IdMso, but in Excel 2013 the Clip Art button’s caption changed to Online Pictures and its IdMso changed to ClipArtInsertDialog. In Excel 2007 and 2010 the IdMso is ClipArtInsert.

So, in order to make our custom group work in Excel 2007 up to Excel 2013, we need to add some logic to change the IdMso, ImageMso and Caption properties of our controls depending on the version of Excel the user is running. We’ll also need to set the ToggleButton property of the control to true, if the user is running Excel 2010 or 2007.

To do this, add the following code in the OnRibbonBeforeCreate event:

private void AddinModule_OnRibbonBeforeCreate(object sender,
    string ribbonId)
{
    switch (this.HostMajorVersion)
    {
        case 12:
            insertScreenshotRibbonGallery.IdMso = "";
            insertScreenshotRibbonGallery.ImageMso = "";
            insertScreenshotRibbonGallery.Visible = false;
 
            insertOnlinePicturesRibbonButton.IdMso = "ClipArtInsert";
            insertOnlinePicturesRibbonButton.ImageMso = "ClipArtInsert";
            insertOnlinePicturesRibbonButton.Caption = "Clip Art";
            insertOnlinePicturesRibbonButton.ToggleButton = true;
            break;
 
        case 14:
            insertOnlinePicturesRibbonButton.IdMso = "ClipArtInsert";
            insertOnlinePicturesRibbonButton.ImageMso = "ClipArtInsert";
            insertOnlinePicturesRibbonButton.Caption = "Clip Art";
            insertOnlinePicturesRibbonButton.ToggleButton = true;
            break;
 
        case 15:
            insertOnlinePicturesRibbonButton.IdMso = "ClipArtInsertDialog";
            insertOnlinePicturesRibbonButton.ImageMso = "ClipArtInsertDialog";
            insertOnlinePicturesRibbonButton.Caption = "Online Pictures";
            break;
 
        default:
            break;
    }
}

Things to keep in mind

Although it is possible to replace most built-in Office controls in the manner I’ve shown you, there are a few controls that simply cannot be imitated using similar techniques. For example, the Excel Paste button, located inside the Clipboard group on the Home tab, cannot be replaced with your own control. Unfortunately, no workaround exists for this.

Another caveat, for which there is no workaround, is that the Office Ribbon offers very little control over how controls are aligned and how they react to the horizontal resizing of the host Office window.

You cannot use the same approach described above for add-ins customizing a built-in group using backdoors, such as Lync in Outlook. You will not be able to use its controls on your own custom Ribbon groups.

Lastly, if two or more add-ins created a custom group to imitate the same built-in group (both add-ins will set visibility to False), then all the custom ribbon groups will be visible. However, a workaround can be achieved by following the approach as described in Sharing Ribbon controls across multiple add-ins.

Thank you for reading. Until next time, keep coding!

Available downloads:

This sample Excel add-in was developed using Add-in Express for Office and .net:

C# sample Excel add-in

You may also be interested in:

6 Comments

  • http://1.gravatar.com/avatar/303bb783b01e7e9838c1fcc6f95c2fd5?s=32&d=http%3A%2F%2F1.gravatar.com%2Favatar%2Fad516503a11cd5ca435acc9bb6523536%3Fs%3D32&r=G Henri Pellemans says:

    Hi Pieter, is there a way to edit the standard Home | Number | NumberFormat drop down list?

    I would like ‘Number’ with a thousands separator but without decimal places, and ‘Accounting’ with a thousands separator and 2 decimal places, but not formatted like Currency (so aligned to the right instead of somewhere in the middle of a cell).

    Of course I could make my own dropdownlist to format cells, however it would be nice if I could use the built-in list of Excel (2010).

    Best regards,

    Henri

  • http://0.gravatar.com/avatar/e1a4c2b21a5186e0b27c1c601f418b76?s=32&d=http%3A%2F%2F0.gravatar.com%2Favatar%2Fad516503a11cd5ca435acc9bb6523536%3Fs%3D32&r=G Pieter van der Westhuizen says:

    Hi Henri,

    The list of Excel ids shows, the control name to be NumberFormatGallery.
    It is possible to add the control to my own group, but it does not seem to be possible to replace the items in the combo box. So, it looks like you would have to add your own dropdown to do it.

    Thanks for your comment!

  • http://1.gravatar.com/avatar/f8e6ecb8669aafd64e6bc460de59ca95?s=32&d=http%3A%2F%2F1.gravatar.com%2Favatar%2Fad516503a11cd5ca435acc9bb6523536%3Fs%3D32&r=G Kevin says:

    Hi Pieter,

    Is there a way to re-map keyboard shortcuts in Excel? For example, in Excel 2010 I can push: Alt+N+P to insert a picture. Is there a way to change it to something like Alt+I+P or even Alt+N+P?

    Also, in the event that I use the above to replace or add an Office control, what happens to the keyboard shortcuts? Is the newly added one assigned a keyboard shortcut? If something is replaced, does it also take on the keyboard shortcuts of what it replaced?

    Really hoping I can make this work for my needs.

    Thanks!

    Kevin

  • http://0.gravatar.com/avatar/e1a4c2b21a5186e0b27c1c601f418b76?s=32&d=http%3A%2F%2F0.gravatar.com%2Favatar%2Fad516503a11cd5ca435acc9bb6523536%3Fs%3D32&r=G Pieter van der Westhuizen says:

    Hi Kevin,

    Unfortunately, the Add-in Express Keyboard shortcut component does not support intercepting multi-step shortcuts such as ALT+N+P.
    This also means, that you cannot re-map shortcuts.

    All the best,
    Pieter

  • http://1.gravatar.com/avatar/5eddd1e5daa51ddeaf5e4196e39cefbd?s=32&d=http%3A%2F%2F1.gravatar.com%2Favatar%2Fad516503a11cd5ca435acc9bb6523536%3Fs%3D32&r=G Chris says:

    Hi, Pieter. I am, in fact, developing an Outlook add-in for scheduling Lync conferences. What I would like to do is to hide the Microsoft “Lync Meeting” group, from within my add-in code, when an inspector opens that contains a meeting scheduled using my add-in. I have all the code in place to properly identify such meetings, wherein I would now like to hide the Microsoft Lync Meeting group, but I seem unable to figure out how to do that. The following c# code seems to invalidate that group, by way of proving that I can identfy it at some level,

    e.RibbonUI.InvalidateControl(“Lync Meeting”); // Doesn’t fail or return an err.

    but I cannot figure out any way to hide it. I don’t seem to have access to it in any way.

    Your post seems to suggest that perhaps this isn’t possible, but I’d like to believe I’m not reading it properly. Do you have any guidance?

    Thank you.

  • http://0.gravatar.com/avatar/e1a4c2b21a5186e0b27c1c601f418b76?s=32&d=http%3A%2F%2F0.gravatar.com%2Favatar%2Fad516503a11cd5ca435acc9bb6523536%3Fs%3D32&r=G Pieter van der Westhuizen says:

    Hey Chris,

    You need the ribbon controls’ ID in order to hide it. I did a quick Google search to see whether Microsoft has published the ids for the Lync add-in somewhere, but was unsuccessful.
    So, unless you have the internal ID you would not be able to hide it.

Post a comment

Have any questions? Ask us right now!