如何在Office文件中嵌入图像以用于自定义功能区

时间:2023-01-20 09:48:47

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?

我正在为Excel开发自定义功能区扩展,其中控件需要不同的自定义图像。我设法使用位于我的文件系统中的一些图像,但我想将这些图像嵌入到.xlsm文件中。是否可以这样做并从更新控件图像的VBA代码中引用它们?

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

出于测试目的,这是定义我的自定义功能区的XML:

<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:

这是更改customButton1控件图像的宏:

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...

我试图在.xlsm中添加bmp文件并引用它们更新关系文件(.rels),但我不知道如何从VBA引用它们,最重要的是,当我用Excel打开文件并保存它时,它们会被自动删除...

Any help is appreciated!

任何帮助表示赞赏!

1 个解决方案

#1


1  

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:

如果图像嵌入在customUI中,则不需要VBA将它们添加到控件中。只需对图像标记中的图像使用相同的ID:

<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).

我的示例是使用ID“TestID”来处理图像,该ID必须在customUI XML中找到 - 在自定义UI编辑器中展开customUI节点以查找或更改图像ID(或使用编辑器添加新图像)。

#1


1  

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:

如果图像嵌入在customUI中,则不需要VBA将它们添加到控件中。只需对图像标记中的图像使用相同的ID:

<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).

我的示例是使用ID“TestID”来处理图像,该ID必须在customUI XML中找到 - 在自定义UI编辑器中展开customUI节点以查找或更改图像ID(或使用编辑器添加新图像)。