vba 如何在 Office 文件中为自定义功能区嵌入图像
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/12686125/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to embed images in Office file for custom Ribbon
提问by Rusty Gear
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!
任何帮助表示赞赏!
采纳答案by Olle Sj?gren
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 将它们添加到控件中。只需对image
标签中的图像使用相同的 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 Editorto find or change the image ID (or use the editor to add a new image).
我的示例使用 ID“TestID”处理图像,该图像必须在 customUI XML 中找到 - 展开自定义 UI 编辑器中的customUI
节点以查找或更改图像 ID(或使用编辑器添加新图像)。
回答by Ray Schr?ter
robcooper's answer from UtterAccess.commight help:
来自UtterAccess.com 的robcooper 的回答可能会有所帮助:
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