当多个插件试图修改功能区时,我在Roi-Kyi Bryant的解决方案中遇到了困难。我的工作计算机上也没有管理员权限,因此无法安装Custom UI Editor。因此,如果您与我同在一条船上,那么这是一个仅使用Excel自定义功能区的替代示例。请注意,我的解决方案来自Microsoft指南。
- 创建要自定义功能区的一个或多个Excel文件。就我而言,我已经创建了两个
.xlam文件,Chart Tools.xlam并且Priveleged UDFs.xlam,演示了如何多的加载项可以与丝带互动。
- 为刚创建的每个文件创建一个具有任何文件夹名称的文件夹。
- 在您创建的每个文件夹内,添加
customUI和_rels文件夹。
- 在每个
customUI文件夹中,创建一个customUI.xml文件。该customUI.xml文件详细说明Excel文件如何与功能区交互。Microsoft指南的第2部分介绍了customUI.xml文件中的元素。
我的customUI.xml档案Chart Tools.xlam看起来像这样
<customUI xmlns="http://schemas.microsoft.com/office/2006/01/customui" xmlns:x="sao">
<ribbon>
<tabs>
<tab idQ="x:chartToolsTab" label="Chart Tools">
<group id="relativeChartMovementGroup" label="Relative Chart Movement" >
<button id="moveChartWithRelativeLinksButton" label="Copy and Move" imageMso="ResultsPaneStartFindAndReplace" onAction="MoveChartWithRelativeLinksCallBack" visible="true" size="normal"/>
<button id="moveChartToManySheetsWithRelativeLinksButton" label="Copy and Distribute" imageMso="OutlineDemoteToBodyText" onAction="MoveChartToManySheetsWithRelativeLinksCallBack" visible="true" size="normal"/>
</group >
<group id="chartDeletionGroup" label="Chart Deletion">
<button id="deleteAllChartsInWorkbookSharingAnAddressButton" label="Delete Charts" imageMso="CancelRequest" onAction="DeleteAllChartsInWorkbookSharingAnAddressCallBack" visible="true" size="normal"/>
</group>
</tab>
</tabs>
</ribbon>
</customUI>
我的customUI.xml档案Priveleged UDFs.xlam看起来像这样
<customUI xmlns="http://schemas.microsoft.com/office/2006/01/customui" xmlns:x="sao">
<ribbon>
<tabs>
<tab idQ="x:privelgedUDFsTab" label="Privelged UDFs">
<group id="privelgedUDFsGroup" label="Toggle" >
<button id="initialisePrivelegedUDFsButton" label="Activate" imageMso="TagMarkComplete" onAction="InitialisePrivelegedUDFsCallBack" visible="true" size="normal"/>
<button id="deInitialisePrivelegedUDFsButton" label="De-Activate" imageMso="CancelRequest" onAction="DeInitialisePrivelegedUDFsCallBack" visible="true" size="normal"/>
</group >
</tab>
</tabs>
</ribbon>
</customUI>
- 对于您在步骤1中创建的每个文件,在
.zip文件名后缀a 。就我而言,我改名Chart Tools.xlam来Chart Tools.xlam.zip,并Privelged UDFs.xlam到Priveleged UDFs.xlam.zip。
- 打开每个
.zip文件,然后导航到该_rels文件夹。将.rels文件复制到_rels您在步骤3中创建的文件夹中。使用文本编辑器编辑每个 .rels文件。从Microsoft指南
在final <Relationship>元素和close
<Relationships>元素之间,添加一行以在文档文件和定制文件之间创建关系。确保正确指定文件夹和文件名。
<Relationship Type="http://schemas.microsoft.com/office/2006/
relationships/ui/extensibility" Target="/customUI/customUI.xml"
Id="customUIRelID" />
我的.rels档案Chart Tools.xlam看起来像这样
<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<Relationships xmlns="http://schemas.openxmlformats.org/package/2006/relationships">
<Relationship Id="rId3" Type="http://schemas.openxmlformats.org/officeDocument/2006/relationships/extended-properties" Target="docProps/app.xml"/><Relationship Id="rId2" Type="http://schemas.openxmlformats.org/package/2006/relationships/metadata/core-properties" Target="docProps/core.xml"/>
<Relationship Id="rId1" Type="http://schemas.openxmlformats.org/officeDocument/2006/relationships/officeDocument" Target="xl/workbook.xml"/>
<Relationship Type="http://schemas.microsoft.com/office/2006/relationships/ui/extensibility" Target="/customUI/customUI.xml" Id="chartToolsCustomUIRel" />
</Relationships>
我的.rels档案Priveleged UDFs看起来像这样。
<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<Relationships xmlns="http://schemas.openxmlformats.org/package/2006/relationships">
<Relationship Id="rId3" Type="http://schemas.openxmlformats.org/officeDocument/2006/relationships/extended-properties" Target="docProps/app.xml"/><Relationship Id="rId2" Type="http://schemas.openxmlformats.org/package/2006/relationships/metadata/core-properties" Target="docProps/core.xml"/>
<Relationship Id="rId1" Type="http://schemas.openxmlformats.org/officeDocument/2006/relationships/officeDocument" Target="xl/workbook.xml"/>
<Relationship Type="http://schemas.microsoft.com/office/2006/relationships/ui/extensibility" Target="/customUI/customUI.xml" Id="privelegedUDFsCustomUIRel" />
</Relationships>
- 将
.rels每个.zip文件中的.rels文件替换为您在上一步中修改的文件。
- 将
.customUI您创建的文件夹复制并粘贴到文件的主目录中.zip。
.zip从您创建的Excel文件中删除文件扩展名。
- 如果已创建
.xlam文件,请返回Excel,将其添加到Excel加载项。
- 如果适用,请在每个外接程序中创建回调。在第4步中,
onAction我的按钮中包含关键字。该onAction关键字指示,含元素被触发时,Excel应用程序将触发子例程后直接封装在引号onAction关键字。这称为回调。在我的.xlam文件中,有一个名为的模块CallBacks,其中包含了我的回调子例程。

我的CallBacks模块Chart Tools.xlam看起来像
Option Explicit
Public Sub MoveChartWithRelativeLinksCallBack(ByRef control As IRibbonControl)
MoveChartWithRelativeLinks
End Sub
Public Sub MoveChartToManySheetsWithRelativeLinksCallBack(ByRef control As IRibbonControl)
MoveChartToManySheetsWithRelativeLinks
End Sub
Public Sub DeleteAllChartsInWorkbookSharingAnAddressCallBack(ByRef control As IRibbonControl)
DeleteAllChartsInWorkbookSharingAnAddress
End Sub
我的CallBacks模块Priveleged UDFs.xlam看起来像
显式期权
Public Sub InitialisePrivelegedUDFsCallBack(ByRef control As IRibbonControl)
ThisWorkbook.InitialisePrivelegedUDFs
End Sub
Public Sub DeInitialisePrivelegedUDFsCallBack(ByRef control As IRibbonControl)
ThisWorkbook.DeInitialisePrivelegedUDFs
End Sub
不同的元素具有不同的回调子例程签名。对于按钮,必需的子例程参数为ByRef control As IRibbonControl。如果您不符合要求的回调签名,则在编译VBA项目时会收到错误消息。Microsoft指南的第3部分定义了所有回调签名。
这是我完成的示例的样子

一些结束提示
- 如果您希望加载项共享Ribbon元素,请使用
idQand xlmns:关键字。在我的示例中,Chart Tools.xlam和Priveleged UDFs.xlam都可以访问具有idQ等于x:chartToolsTab和的元素x:privelgedUDFsTab。为此,x:必须这样做,并且我已经在customUI.xml文件的第一行中定义了其命名空间<customUI xmlns="http://schemas.microsoft.com/office/2006/01/customui" xmlns:x="sao">。Microsoft指南中的两种自定义Fluent UI的方法一节提供了更多详细信息。
- 如果您希望加载项访问Excel附带的Ribbon功能区,请使用
isMSO关键字。Microsoft指南中的两种自定义Fluent UI的方法一节提供了更多详细信息。