View Full Version : How to grey out a Ribbon Bar button

10-14-2018, 11:42 AM
I have a template which allows me to change the caption numbering for tables and figures from the standard 1, 2 ,3, etc to that of Chapter numbering such as 1.1, 1.2, 1.3 and so on.

Thanks to Paul Hossler, who, four years ago, answered a post and placed a very practical solution on this forum. I have taken the liberty of using his code and my template works as I want it to.

There is one "However": If I create a new document from the template and select the button on the Ribbon Bar which says "Set Chapter Captions" it changes to the correct Chapter numbering as above and also sets a Custom Document Property (Boolean) to 'True' indicating that such an action has been taken. I can then save that document for future use with the Document Property set in it.

When I next open that document, and as Chapter numbering has been set, I would like the Ribbon Button to be greyed out indicating that no further action is possible.

At the moment, what happens, the saved document opens but the Ribbon Bar button appears to be active. I then click on it and the programming seems to say "Ah, yes, you have set Chapter Numbering because you have a Custom Document Property telling me so so I will turn the button to grey"

This does happen.

What I want the programming to do is to read that Custom Property in the document on loading and change the Ribbon Bar button from active to greyed out without any further action from me.

This is a snip of the Ribbon Bar:


Here is the template XML:

<!--Create Chapters-->

<button id="rxbtnConvertToChapters"

In the VBA Editor, this is the code on loading the Ribbon:

Sub rxIRibbonUI_OnLoad(ByVal ribbon As Office.IRibbonUI)
Set grxIRibbonUI = ribbon
'now goes back to the top of the document
bBeenRun = False
End Sub

This is the code behind the ConvertToChapters procedure:

Sub rxbtnConvertToChapters_Click(control As IRibbonControl)

If ActiveDocument.CustomDocumentProperties("Chapter Numbers") = True Then
bBeenRun = True
grxIRibbonUI.InvalidateControl ("rxbtnConvertToChapters")
End If

End Sub

And here is the code for the callback "AlreadyRun" from the XML getEnabled

'Callback for AlreadyRun
Sub AlreadyRun(control As IRibbonControl, ByRef returnedVal)
returnedVal = Not bBeenRun
End Sub

I have tried various combinations of the 'bBeenRun' setting to try and get the Convert To Chapters to go grey if Chapter Heading CustomDocumentProperty has been set to 'True'.

Could somebody be able to point me in the right direction, please?



10-16-2018, 05:40 PM
Just a quick guess ...

Sub rxIRibbonUI_OnLoad(ByVal ribbon As Office.IRibbonUI)
Set grxIRibbonUI = ribbon
'now goes back to the top of the document
bBeenRun =
ActiveDocument.CustomDocumentProperties("Chapter Numbers")
End Sub