VBA - 自定义功能区 - 在添加插件时

3

我编写了一个程序想要分发给我的同事,但他们不熟悉VBA。因此,我希望能够轻松地分发它。

我创建了一个插件,当安装插件时需要创建自定义菜单选项卡(Ribbon)。我已经尽力了,但是我找不到易于理解的文档。

我的代码如下:

Private Sub Workbook_AddinInstall()

On Error Resume Next 'Just in case

    Application.CommandBars("Worksheet Menu Bar").Controls("Super Code").Delete

    Set cControl = Application.CommandBars("Worksheet Menu Bar").Controls.Add
        With cControl
            .Caption = "Open SCF workbook"
            .Style = msoButtonIconAndCaptionBelow
            .OnAction = "OpenTheCorrectFile"
            .FaceId = 7720
            .DescriptionText = "Open the SCF workbook"
        End With
               
    Set cControl = Application.CommandBars("Worksheet Menu Bar").Controls.Add
        With cControl
            .Caption = "Are they onboard"
            .Style = msoButtonIconAndCaptionBelow
            .FaceId = 5817
            .OnAction = "Check_Suppliers_Already_On_Board"
            .DescriptionText = "Check if suppliers have already been on boarded"
        End With
End Sub

现在如果您使用我的代码,您会注意到:

  • 按钮很小
  • 图标很小
  • 悬停时没有描述
  • 新功能区没有特殊名称,它被称为"Add-ins"
  • 这个组没有特殊名称,它被称为"菜单命令"

有人知道如何解决这个问题。 每当我寻找这些答案时,人们都使用特殊的应用程序来创建功能区,但我不想这样做。我也是VBA的初学者,所以任何易于理解的内容都受欢迎。

非常感谢。


2
有一个TooltipText属性可用于设置悬停描述(可用属性列表:https://learn.microsoft.com/en-us/office/vba/api/overview/library-reference/commandbarbutton-members-office) - Ivan
还有宽度和高度属性,但我无法使它们起作用(按钮大小没有改变)。显然,更好的自定义现在需要 XML、Microsoft Office 的自定义 UI 编辑器或类似工具。我认为仅使用 VBA 是不可能的。 - Ivan
这里有一个很好的例子,可以在没有任何应用程序的情况下完成,但您需要手动编辑customUI.xml文件和xlsm文件结构(重命名为.zip,然后编辑/添加一些文件,再次重命名为.xlsm):https://learn.microsoft.com/en-us/office/vba/library-reference/concepts/customize-the-office-fluent-ribbon-by-using-an-open-xml-formats-file - Ivan
非常感谢你,伊万。我会尝试你在这里给我的建议。还有感谢你提供的工具提示属性。 - VI55
1个回答

1
这里有一个例子,演示如何在不使用任何外部应用程序的情况下完成它,只需使用文本编辑器。但是您需要小心地编写XML代码,否则Excel文件可能无法正确打开(请备份副本)。
  1. You will create a folder named "customUI" and, inside that folder, a file named "customUI.xml"

  2. You will write all your Ribbon details inside "customUI.xml". I wrote this example, covering the issues you listed: Big buttons and icons (Images list in https://bert-toolkit.com/imagemso-list.html) / Tooltip description (screentip and supertip) / Ribbon name (tab label) / Group names (group labels)

    <customUI xmlns="http://schemas.microsoft.com/office/2006/01/customui">  
    <ribbon>
      <tabs> 
        <tab id="CustomTab" label="Add-ins"> 
          <group id="Group1" label="Menu Commands"> 
            <button id="Button1" label="Happy" size="large" screentip="Test1" supertip="Test1 description" imageMso="HappyFace" onAction="Macro1" /> 
            <button id="Button2" label="Save" size="large"  imageMso="FileSave" onAction="Macro2" /> 
            <button id="Button3" label="{a}" size="large" imageMso="FieldCodes" onAction="Macro3" /> 
          </group > 
          <group id="Group2" label="Other Commands"> 
            <button id="Button4" label="Reply" size="large" imageMso="Reply" onAction="Macro4" /> 
          </group > 
         </tab> 
      </tabs> 
    </ribbon> 
    </customUI> 
    
  1. Rename your .XLSM file to .ZIP, then you will be able to edit/add files. Add the customUI folder to the zip file.

  2. From the ZIP file, extract the .rels file that is inside _rels folder, you will add this line between the last Relationship tag and the Relationships closing tag, and then put it back inside the ZIP file overwriting the .rels file there

     <Relationship Id="someID" Type="http://schemas.microsoft.com/office/2006/relationships/ui/extensibility" Target="customUI/customUI.xml" /> 
    
  3. Rename the file back to .XLSM and open it, if everything is right, the new Ribbon will be there. It's important that the macros that will be called by the buttons have a control parameter, like this:

     Sub Macro3(ByVal control As IRibbonControl)
    

这就是全部,与UI相关的所有内容都在customUI.xml文件中,不再需要在VBA中添加按钮。如果您需要更改UI中的任何内容,则必须将其重新命名为ZIP,覆盖customUI.xml文件,然后再次将其重命名为XLSM。以下是我在此示例中获得的功能区:

Ribbon Menu Test


1
Ribbon按钮属性参考:https://learn.microsoft.com/zh-cn/openspecs/office_standards/ms-customui/846e8fb6-07d3-460b-816b-bcfae841c95b - Ivan
我不知道如何编写XML代码,我想我必须学习它!非常感谢您详细的答案,它非常有帮助。 - VI55
不客气!还有一个细节:我注意到当我测试XML示例时,重复的ID会导致错误,这就是为什么我将按钮命名为“Button1”,“Button2”等的原因。如果您创建了许多组和按钮,请确保避免重复的ID。 - Ivan

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接