【发布时间】:2014-04-16 21:56:53
【问题描述】:
PHB 希望我为 Microsoft Word 创建一个功能区切换按钮:
- 按下时,将编辑限制为填写表格并在没有密码的情况下保护文档。
- 未按下时,取消对文档的保护(无需密码)。
我有以下 customUI.xml:
<customUI xmlns="http://schemas.microsoft.com/office/2006/01/customui" onLoad="RibbonOnLoad">
<commands>
<command idMso="ProtectOrUnprotectDocument" onAction="ProtectOrUnprotectDocumentOnAction"/>
</commands>
<ribbon>
<tabs>
<tab id="LawTab" label="Law">
<group id="ProtectGroup" label="Protect">
<toggleButton id="ToggleProtectionButton" imageMso="GreenBall" label="Protection" getPressed="ToggleProtectionButtonGetPressed" onAction="ToggleProtectionButtonOnAction"/>
<button id="InvalidateRibbonButton" imageMso="Refresh" label="Invalidate Ribbon" onAction="InvalidateRibbonButtonOnAction"/>
</group>
</tab>
</tabs>
</ribbon>
</customUI>
以及以下 VBA 代码:
Private ribbon As IRibbonUI
Sub InvalidateRibbonButtonOnAction(control As IRibbonControl)
ribbon.Invalidate
End Sub
Sub ProtectOrUnprotectDocumentOnAction(control As IRibbonControl, ByRef cancelDefault)
ribbon.Invalidate
cancelDefault = False
End Sub
Sub RibbonOnLoad(ActiveRibbon As IRibbonUI)
Set ribbon = ActiveRibbon
End Sub
Sub ToggleProtectionButtonGetPressed(control As IRibbonControl, ByRef returnValue)
returnValue = ActiveDocument.ProtectionType <> wdNoProtection
End Sub
Sub ToggleProtectionButtonOnAction(control As IRibbonControl, ByVal pressed As Boolean)
If pressed Then
ActiveDocument.Protect wdAllowOnlyFormFields
Else
ActiveDocument.Unprotect
End If
End Sub
我可以重新调整内置 ProtectOrUnprotect 命令的用途,以便相应的内置按钮使功能区无效并因此更新我的自定义按钮,但如果我使用内置任务窗格(查看 > 限制编辑)或以编程方式保护/取消保护(ActiveDocument.Protect/Unprotect),我的自定义按钮对更改一无所知。如何监听文档级事件以保护/取消保护,以便更新切换按钮的状态?
【问题讨论】:
-
+1 提出一个好问题。用于功能区操作的资源并不多。如果我有几分钟的空闲时间,我会看看我是否能弄清楚......
-
请在
ToggleProtectionButtonGetPressed函数中插入一个消息框。您是否可以通过 Word 中的用户操作调用此功能?我不能这样做。如果你是,请告诉我你是怎么做的,我会继续努力的。否则,您的解决方案可能需要类似于 PowerPoint (here) 所需的加载项,如果甚至可以响应此特定事件(可能不是)... -
我创建了一个无效功能区按钮。要对其进行测试,请使用内置任务窗格(查看 > 限制编辑)来启动/停止保护。在您按下“无效功能区”按钮之前,我的自定义按钮将忽略实际的保护状态。
标签: vba ms-word ribbon ribbonx