尝试构建Excel功能区时未显示自定义图标

时间:2020-03-24 14:02:10

标签: excel vba icons ribbon

我正在尝试制作一个AddIn,该AddIn在安装/打开后会自动使用自定义宏和图标构建功能区。

到目前为止,在选择/打开AddIn时,我已经能够构建功能区,分配给按钮上的宏可以正常工作,我的问题是我试图为每个宏按钮使用自定义图标,没有显示。我怀疑在构建功能区时这是一个路径问题,但是我的VBA知识不够广泛,无法验证和修正此问题。

我正在使用xml和vba模块中存在的回调函数来在构建功能区时加载图像。图像是.png文件,据我了解,使用LoadPicture函数时excel加载时可能会出现问题,但我尝试使用.jpg文件进行了尝试,但仍然无法正常工作。

这是使用的xml代码

    <?xml version="1.0" encoding="UTF-8"?>
<customUI onLoad="Ribbon_Load" xmlns="http://schemas.microsoft.com/office/2006/01/customui" loadImage="CallbackLoadImage">
    <ribbon>
        <tabs>
            <tab id="AutoReport" label="AutoReport" insertAfterMso="Developer">
                <group id="group1" label="General">
                    <button id="button1" onAction = "New_Slide_Button" description="Create a new slide in the presentation" label="New Slide" size="large" image= "New_Slide.png"/>
                    <button id="button2" onAction = "Apply_Theme_Button" description="Apply theme to presentation" label="Apply Theme" size="large" image="Apply_Theme.png" />
                    <button id="button3" onAction = "Apply_Theme_Button.png" description="Apply formatting to presentation" label="Apply Formatting" size="large" image="Apply_Formatting.png" />
                </group>
                <group id="group2" label="Styling">
                    <dropDown id="dropDown1" label="Theme" image="Theme.png" />
                    <dropDown id="dropDown2" label="Font" image="Font.png" />
                </group>
                <group id="group3" label="View">
                    <button id="button4" onAction = "New_Preview_Button" description="Create new preview of presentation" label="New Preview" image="Clean_Preview.png" />
                    <button id="button5" onAction = "Save_Preview_Button" description="Save preview formatting and layout of presentation" label="Save Preview" image="Save.png" />
                    <button id="button6" onAction = "Preview_Button" label="Preview" image="Preview.png"/>
                </group>
                <group id="group4" label="Command">
                    <button id="button7" onAction ="Build_Presentation_Button" description="Create PowerPoint Presentation" label="Create Presentation" size="large" image="Create_Presentation.png" />
                </group>
            </tab>
        </tabs>
    </ribbon>
</customUI>

用于构建功能区的VBA代码如下:

Sub Build_Ribbon()

Dim hFile As Long
Dim path As String, fileName As String, ribbonXML As String, user As String
Dim my_file As Integer
Dim text_line As String
Dim file_name As String
Dim i As Integer

hFile = FreeFile
user = Environ("Username")
path = "C:\Users\" & user & "\AppData\Local\Microsoft\Office\"
fileName = "Excel.officeUI"

    ribbonXML = ""
    file_name = "C:\Users\" & user & "\AppData\Roaming\Microsoft\AddIns\Smart Report\AutoReportXML.txt"
    my_file = FreeFile()
    Open file_name For Input As my_file

    i = 1

    While Not EOF(my_file)
        Line Input #my_file, text_line
        ribbonXML = ribbonXML & text_line & vbNewLine
        i = i + 1
    Wend

Debug.Print ribbonXML

Close hFile
Open path & fileName For Output Access Write As hFile
Print #hFile, ribbonXML
Close hFile

'ribbonXML = "<mso:customUI           xmlns:mso=""http://schemas.microsoft.com/office/2009/07/customui"">" & _
"<mso:ribbon></mso:ribbon></mso:customUI>"

End Sub

用于加载图像的回调如下:

Sub CallbackLoadImage(imageID As String, ByRef image)
Dim ThisPath As String
ThisPath = Application.ActiveWorkbook.path

 Set image = LoadPicture(ThisPath & "\Icons\" & imageID)
End Sub

所有图像都位于同一目录的Icons文件夹中。不知道我要去哪里错了。

任何帮助将不胜感激。

0 个答案:

没有答案
相关问题