Home > Back-end >  Enable/Disable custom ribbon commands in MS Word
Enable/Disable custom ribbon commands in MS Word

Time:02-17

I have produced a ribbon in MS Word with custom macros associated with various buttons to automate a checking process. As part of this checking process, it needs to go to Person A before Person B can sign off on it.

To add rigor to this I want to disable the "Person B Sign Off" button in the ribbon until Person A's has been completed, this as well as disabling various other buttons with conditional triggers. I have looked into multiple avenues but nothing has even come close to working with articles I've found, I assume I need to edit the XML but what and how I'm not sure.

[

For example - Retrieve LLC would be greyed out until LLC has actually filled in comments

Any pointers or help would be really appreciated!

CodePudding user response:

On the one hand you have to edit your XML - e.g.

 <button id ="btnLLCA" onAction="btnLLCA_onAction" 
                       getEnabled="btnLLCA_getEnabled"
                       getVisible="btnLLCA_getVisible"
                       />

getEnabled and getVisible are the keywords you were looking for. The text after the equal sign is the name of the sub in your vba-code - like with onAction

And then create the according Subs in your VBA-Code:

'Callback for btnLLCA_getEnabled
Sub btnLLCA_getEnabled(control As IRibbonControl, ByRef returnedVal)
    returnedVal = hasPersonAReviewed   -->adjust to your needs
End Sub

'Callback for btnLLCA_getVisible
Sub btnLLCA_getVisible(control As IRibbonControl, ByRef returnedVal)
    returnedVal = hasProcessStarted  --> adjust to your needs
End Sub

So - what you have to do: return a returnedVal according to the status of your process.

CodePudding user response:

In general, you can customize the Ribbon UI by using callback procedures in COM add-ins. For each of the callbacks that the add-in implements, the responses are cached.

For example, if an add-in writer implements the getEnabled callback procedure for a button, the function is called once, the status is returned, and then if the enabled status needs to be updated, the cached value is used instead of recalling the procedure. This process remains in place until the add-in signals that the cached values are invalid by using the Invalidate method, at which time, the callback procedure is again called and the return response is cached. The add-in can then force an immediate update of the UI by calling the Refresh method from VSTO add-ins. But for VBA macros you can use the Invalidate or InvalidateControl methods of the IRibbonUI interface. To get an instance of the IRibbonUI interface you need to define the onLoad callback in the ribbon XML:

<customUI … onl oad="MyAddinInitialize" …>

And you VBA macro could look like that:

Dim MyRibbon As IRibbonUI 
 
Sub MyAddInInitialize(Ribbon As IRibbonUI) 
 Set MyRibbon = Ribbon 
End Sub 
 
Sub myFunction() 
 MyRibbon.Invalidate() ' Invalidates the caches of all of this add-in's controls 
End Sub

You need to use the getEnebled callbacks in the ribbon XML markup to be able to invalidate the control's state - enabled or not. The signature of the getEnabled callback looks in the following way:

Sub GetEnabled(control As IRibbonControl, ByRef enabled)

You can find the Fluent UI (aka Ribbon UI) described in depth in the following series of articles:

  • Related