The PHB wants me to create a ribbon toggle button for Microsoft Word that:
- when pressed, restricts editing to filling in forms and protects the document without a password.
- when unpressed, unprotects the document (without a password).
I have the following 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>
and the following VBA code:
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
I can repurpose the built-in ProtectOrUnprotect command so the corresponding built-in button invalidates the ribbon and consequently updates my custom button, but if I protect/unprotect with the built-in task pane (Review > Restrict Editing) or programmatically (ActiveDocument.Protect/Unprotect), my custom button stays ignorant of the change. How can I listen to the document-level event to protect/unprotect so I can update the state of my toggle button?
ToggleProtectionButtonGetPressed
function. Are you able to invoke this function through user action in Word? I am not able to do so. If you are, please let me know how you do it and I will keep trying. Otherwise, your solution may require an Add-In similar to what is needed for PowerPoint (here), IF it is even possible to respond to this particular event (it may not be)... – David Zemens