6
votes

I am developing a custom ribbon extension for Excel, in which a control requires different custom images. I managed to use some images located in my filesystem, but I would like to embed these images inside the .xlsm file. Is it possible to do it and to reference them from the VBA code that updates the image of the control?

For test purposes, this is the XML that defines my custom ribbon:

<customUI xmlns="http://schemas.microsoft.com/office/2006/01/customui" onLoad="ribbonLoaded">
  <ribbon>
    <tabs>
      <tab idMso="TabHome" >
        <group id="customGroup1" label="My Group" insertAfterMso="GroupFont">
          <button id="customButton1" label="Click Me" size="large" onAction="Macro1" getImage="getButtonImage"/>
        </group>
      </tab>
    </tabs>
  </ribbon>
</customUI>

And this is the macro that change the image of the customButton1 control:

Dim imgIndex As Long

Public Sub getButtonImage(ByVal control As IRibbonControl, ByRef Image)
Select Case control.ID
  Case "customButton1"
    Set Image = LoadPicture("img" + Trim(Str(imgIndex)) + ".bmp")
    imgIndex = (imgIndex + 1) Mod 2
  End Select
End Sub

I tried to add the bmp files inside the .xlsm and reference them updating the relationships file (.rels), but I don't know how to reference them from VBA and most important, when I open the file with Excel and save it, they are automatically deleted...

Any help is appreciated!

2
Do you need to add a few images once or do you need to update the file with new images several times? If you only need to add them once you can add them into the customUI XML by using the Custom UI Editor. If you need to update them often, that method might not work as well.Olle Sjögren
I only need to add the images once. The Custom UI Editor was very useful to solve the first problem of embedding them (manually I was referencing the images in the wrong .rels file...). But how can they be accessed and loaded in VBA? I think the LoadPicture function does not work with them...Rusty Gear

2 Answers

1
votes

If the image is embedded in the customUI, you do not need VBA to add them to a control. Just use the same ID for the image in an image tag:

<button id="button1" label="Test" size="large" image="TestID" onAction="ButtonOnAction" />

My sample is adressing the image with ID "TestID", which must be found in the customUI XML - expand the customUI node in the Custom UI Editor to find or change the image ID (or use the editor to add a new image).

0
votes

robcooper's answer from UtterAccess.com might help:

Public Sub getButtonImage(ByVal control As IRibbonControl, ByRef image)
'for use in Access 2007 Ribbon control
'requires a reference to the Micrsoft Office 12.0 Object Library
    Select Case control.ID
    Case "cmdMainMenu"
        Set image = LoadPicture(CurrentProject.Path & "\home.bmp")
    End Select
End Sub