Avatar of MonteDelMar
MonteDelMar
 asked on

Customise Word 365 Ribbon Tab using VBA

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.
UI/UXVBAMicrosoft Word

Avatar of undefined
Last Comment
MonteDelMar

8/22/2022 - Mon
John Korchok

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
MonteDelMar

ASKER
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 Lambert

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.
Experts Exchange is like having an extremely knowledgeable team sitting and waiting for your call. Couldn't do my job half as well as I do without it!
James Murphy
John Korchok

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.
ASKER CERTIFIED SOLUTION
Fabrice Lambert

THIS SOLUTION ONLY AVAILABLE TO MEMBERS.
View this solution by signing up for a free trial.
Members can start a 7-Day free trial and enjoy unlimited access to the platform.
See Pricing Options
Start Free Trial
GET A PERSONALIZED SOLUTION
Ask your own question & get feedback from real experts
Find out why thousands trust the EE community with their toughest problems.
MonteDelMar

ASKER
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.