Microsoft Excel
Back to jkp-ads.com

Ron de Bruin
Excel Automation

Microsoft MVP Program

Images on Custom Ribbon controls in Excel 2007-2016

Important message to visitors of this page

Ron de Bruin decided to remove all Windows Excel content from his website for personal reasons. If you want to know why, head over to rondebruin.nl.

Luckily, Ron was kind enough to allow me to publish all of his Excel content here.

Most of these pages are slightly outdated and may contain links that don 't work. Please inform me if you find such an error and I'll try to fix it.

Kind regards

Jan Karel Pieterse


On this page I show you a few ways to display the image you want on a custom control on the Ribbon. The code examples are working in Excel 2007-2016, also in the 64 bit versions of Excel.

Before we start we first download the example workbooks and images:
Download the example workbooks and images

Note: If you are not familiar with the Custom UI Editor read the information on this page first.

 

Use Built-in images

Open the workbook Built-in Image.xlsm to test the example below

I use the RibbonX below to add the custom button.
<button id="customButton1" label="Label 1" size="large" onAction="Macro1" imageMso="HappyFace" />

You see that I use imageMso="HappyFace" to tell Excel which image I want to see on the button.

There are a lot of built-in images that you can use in your RibbonX, but how do you find the image names?
Check out the downloads in the section "Find the names of the button images" on this page

 

Use Custom images

Example 1 : Open workbook Custom Image 1.xlsm to test the example below

I add the picture ron.png into the Excel file with the "Insert Icons" option in the Custom UI Editor.

I use the RibbonX below to add the custom button.
<button id="customButton1" label="Label 1" size="large" onAction="Macro1" image="ron" />

You see that I use image="ron" to tell Excel which image I want to see on the button.

Example 2 :Open workbook Custom Image 2.xlsm to test the example below

In Example 1 the pictures are a part of the Excel file but we can also load pictures that are in another location. In this example the pictures must be in the same location as the Excel file. You see that the files Lightoff.png and Lighton.png that I use are in the same folder as the example files.

I use the Ribbon attribute getImage with a VBA callback to display custom images on the buttons. In the callback we call the function LoadPictureGDI from Stephen Bullen to load the pictures. You can find all the code from Stephen Bullen in the MLoadPictureGDI module of the workbook. The code is also working now in Excel 64 bit thanks to Rob Bovey. I also use the Ribbon attribute getLabel with a VBA callback to display labels on the buttons.

See the GetImage and GetLabel callbacks in the VBA editor.

 

Change Image with VBA after you open the file

In the three examples below I use a toggle button, but you can use other controls also. The image will change when the button is pressed or not pressed. I use the Ribbon attribute getImage with a VBA callback to display custom images on the buttons.

Example 2 and 3 are using the LoadPictureGDI function from Stephen Bullen to load the images. You can find all the code from Stephen Bullen in the MLoadPictureGDI module of the workbook. The code is also working now in Excel 64 bit thanks to Rob Bovey.

Note: Look good at the RibbonX and the VBA code in the files below.

Toggle Example 1 : Open workbook Toggle Button 1.xlsm to test the example

In this example I use two built-in images.
There are a lot of built-in images that you can use in your RibbonX, but how do you find the image names?. Check out the downloads in the section "Find the names of the button images" on this page

Toggle Example 2 : Open workbook Toggle Button 2.xlsm to test the example

In Example 1 the pictures are built-in images but we can also load custom images.
In this example the pictures must be in the same location as the Excel file.
You see that the files Lightoff.png and Lighton.png that I use are in the same folder as the example files.

Toggle Example 3 : Open workbook Toggle Button 3.xlsm to test the example below.

In this example I use custom images that are in the Excel file. I have add the pictures into the Excel file with the "Insert Icons" option in the Custom UI Editor. It doesn't seem possible to use the Ribbon attribute getImage with a VBA callback to display a custom image from the customUI\images folder in the Excel file on a Ribbon control.

But you can use the workaround in this example file.

Note: When you open the workbook it will automatic do the things below

1) It will first make a copy of the Excel file.
2) Then it changes the extension of the copy of the Excel file to .zip
3) Then it Unzips the files in this zip file into a normal folder
4) Then it uses the attribute getImage to load the pictures from that normal folder.
5) When you deactivate the workbook it will delete the temporary folder/file