We help IT Professionals succeed at work.

Customise Word 365 Ribbon Tab using VBA

MonteDelMar
MonteDelMar asked
on
Hi Experts

I have a reasonable amount of experience in customising the Ribbon using the CustomUI editor and using VBA to dynamically populate menus. However, I just cannot seem to get my head around what I need to do to add a new Menu to an existing custom Tab using VBA.

I want to have a blank, already created Tab - I have already done that in the Custom UI editor.

I want to programmatically add new menu items to that Tab using VBA.

It would be useful to be able to know how to add a Tab programmatically as well but that is not essential.

Many thanks in advance for your help.
Comment
Watch Question

John KorchokProduction Manager

Commented:
XML mods are normally outside the realm of what VBA can do. The only exception is if you code a class module that can unzip the .docx to XML files. Then your code can open the XML and process it. Finally, your class module re-zips the files into a Word document. You can download such a class module from the web site of Jan Karel Pieterse: Editing elements in an OpenXML file using VBA. This file was written in Excel, but you can modify it for use in Word.

This is not a trivial project, since you also have to take care of all the housekeeping bits like editing .rels files to refer to any added XML. The CustomUI editor does this for you. While the learning curve is steep, in the long run it's easier to use C# or Visual Basic, Visual Studio and the Open XML SDK to programmatically add Ribbon mods.

BTW, you might be interested in a newer Ribbon editor that is available: RibbonX Editor

Author

Commented:
Thanks for your reply, John.

I do agree that this would be better using C# or VB etc. At the moment though, I need to do something in VBA.

I am not sure if I expressed my requirement clearly. In this particular case, the Tab(s) are always loaded when Word starts - they do not vary between documents.

Currently, I have a menu item (defined in the CustomUI editor ) and I populate that using the code below@

'Callback for mnuPresgetContent
Sub mnuPres_getContent(Control As IRibbonControl, ByRef content)
    content = MenuList("qPres")
End Sub

Open in new window


'MenuList' is a routine that writes the XML string for the content.

'qPres' is an Access query that returns the list of items to be added to the menu, and the type if control eg a button or another menu level.

I was wondering if I can actually add the main menu item (the one that is in the CustomUI) to the Tab programmatically. I guess not given your answer above?

Thank you too for the link to the new editor.
Fabrice LambertConsulting
Distinguished Expert 2017

Commented:
I was wondering if I can actually add the main menu item (the one that is in the CustomUI) to the Tab programmatically. I guess not given your answer above?
Nope, the menu must be declared in the ribbon, only its content can be dynamic (in other words, you can't dynamically insert items within ribbon's tab).

On the other hand, you can play with the visibility.
John KorchokProduction Manager

Commented:
If you must use VBA, then the Open XML class module from JKP is your only option. If I get some time, I'll try to put together a Word version.
Consulting
Distinguished Expert 2017
Commented:
If you must use VBA, then the Open XML class module from JKP is your only option. If I get some time, I'll try to put together a Word version.
Better use an editor,
Handling complex XML with VBA is a pain.

Author

Commented:
John, please don't spend any more time on this. I have a workaround for the moment and will aim to use VB going forward. Thank you for your help.