4
votes

I have done some quite extensive customisation to the Office 2010 ribbon in Microsoft Word, using a combination of XML, VBA - using the Custom UI Editor.

What I'm trying to establish is that if it's possible to add a button to the ribbon based on if there is a certain string found in the current file name. For example:

  • If fileName contains "PM" (probably using the InStr method)
  • Add button to ribbon

Any pointers, examples or articles would be much appreciated. I've done some digging but haven't been able to find an appropriate method yet.

I was hoping to use the Onload attribute in the XML to fire the relevant sub that detects the filename and manipulates the ribbon accordingly.

Many thanks in advance.

2
AFAIK, You cannot customize ribbon via VBA. I have answered a similar query here. stackoverflow.com/questions/8850836/….Siddharth Rout
Thanks. The walkthrough provided there is a concept that I am already very familiar with. I was thinking that perhaps I could use the 'onload' element in the XML to fire a VBA sub that detects the file name and adds a button to the ribbon accordingly - but seems that it may not be possible.user1064180
Even if you could do it with VBA, the moment you open the file to run the VBA, the file becomes locked and hence you will not be able to modify the XML of the file.Siddharth Rout
I have not explored the option of doing this from VBS though. But then I guess the main objective will fail :)Siddharth Rout

2 Answers

3
votes

Yes. You can change the layout of the Ribbon with VBA during runtime.

You will have to add the control in the customUI-xml, then add a getVisible-tag within the control that references a VBA-function - you can get the correct signature for the VBA-function from the Custom UI Editor. The function then returns a boolean, True if you want the control to show, and False if not. You can evaluate filename or anything else you want, then return the desired value.

Example customUI:

<button id="btnTest" label="Try me" imageMso="FileMarkAsFinal" size="large" supertip="I dare you!" getVisible="GetBtnTestVisible" />

Example VBA:

'Callback for btnTest getVisible
Sub GetBtnTestVisible(control As IRibbonControl, ByRef returnedVal)
    'Evaluate and set returnedVal accordingly
    returnedVal = True  'Control visible
    returnedVal = False 'Control hidden
End Sub
0
votes

get the filename by grabbing the command line: My Answer on Super User
Then you should be able to set the visible status of your toolbar button dependent on what is returned from that routine