【发布时间】:2020-12-01 20:56:06
【问题描述】:
我正在尝试在 Word 2019 功能区中获取切换按钮。
我在 Stack Overflow 上查看了很多示例,但我没有让它运行。
我的 XML 看起来像:
<customUI xmlns="http://schemas.microsoft.com/office/2009/07/customui" onLoad="loadRibbon">
<ribbon>
<tabs>
<tab id="doc_management" label="Publishing" insertBeforeQ="TabDeveloper">
<group id="doc_drafting" label="Drafting" autoScale="true">
<toggleButton id="toggling" label="Insert Watermark" imageMso="WatermarkGallery" onAction="togglingWatermark" getPressed="buttonPressed"/>
</group>
</tab>
</tabs>
</ribbon>
</customUI>
对应的VBA代码:
Option Explicit
Public myRibbon As IRibbonUI
Public isPressed As Boolean
Sub loadRibbon(ribbon As IRibbonUI)
Set myRibbon = ribbon
End Sub
Sub togglingWatermark(control As IRibbonControl, pressed As Boolean)
Select Case control.ID
isPressed = pressed
Case "toggling"
If isPressed Then
MsgBox isPressed
Else
MsgBox isPressed
End If
End Select
myRibbon.InvalidateControl control.ID
End Sub
Sub buttonPressed(control As IRibbonControl, ByRef returnedVal)
Select Case control.ID
Case "toggling"
returnedVal = isPressed
End Select
End Sub
存在带有相应按钮的自定义选项卡。我也可以切换按钮。但我希望在切换后立即看到一个弹出屏幕。这并没有发生。 我错过了什么?任何帮助将不胜感激!
编辑:
似乎没有加载 IRibbonUI 的实例。所以它不允许我首先使回调无效或使用回调。我通过在loadRibbon Sub 中添加MsgBox("Loaded") 来验证这一点。据我了解,Word 文档一打开就会出现一个消息框。
VBA 代码保存在启用宏的 Word 模板中,并存储在 %appData%\Microsoft\Word\STARTUP 中。添加一个从同一模板调用 Sub 的普通按钮正在工作。
【问题讨论】: