Avatar of Bryce Bassett
Bryce BassettFlag for United States of America

asked on 

Is there a Ribbon Menu Tab Change Event in Microsoft VBA?

I have developed a Global Template in Microsoft Word, a .dotm that sits in the user's AppData\…\Word\STARTUP folder, so it gets loaded every time the user launches MS Word.  The .dotm creates a custom tab in the Word ribbon menu, a toolbar containing numerous automation macro buttons.

I need to do certain checks each time they use the global template, such as making sure they have the latest version, etc.  I am currently triggering these checks by way of the RibbonOnLoad event.  Problem is the ribbon loads when the user first launches the Word application.  

I would rather trigger these checks only when the user switches to my custom tab. Is there a Word VBA Event that fires when the user changes to a certain ribbon menu tab?

My research suggests there is no such event.  But I want to make sure.  If not, is there a workaround to create this same functionality, so I don't have to call the checks at the beginning of every single macro?

Thanks!
Microsoft OfficeVBAMicrosoft Word

Avatar of undefined
Last Comment
Bryce Bassett
Avatar of ste5an
ste5an
Flag of Germany image

There is no such event.

If not, is there a workaround to create this same functionality, so I don't have to call the checks at the beginning of every single macro?
Sure, simply use a private Boolean variable. Set it to True after you run your checks. And add a guard clause to each method. This allows you to run your macros be invoking them also by Run Macro dialog or the command line.

When you really want to use an event, than add a getVisible or getEnabled event handler to your groups in ribbon XML and your code behind. They are called the first time a group is shown after the start or an invalidate call affecting the group.
Avatar of Bryce Bassett

ASKER

Thanks for your reply, Ste5an.  However, I do not understand how your suggestion solves my problem.

If I put a getVisible event handler in a ribbon XML group, that event is triggered when the global template first loads, which happens when Word is launched.  This is what I am trying to avoid.  I don't want to run my checks until the user switches to my custom tab or runs one of my macros.

Perhaps I have misunderstood you.   Can you please provide more details?

Thanks
Avatar of ste5an
ste5an
Flag of Germany image

Test it, cause it is only triggered, when your tab is the first time selected. You can trigger it later again by invalidating the group.

The question is: How often do you want to call those checks.

When you want to run it once, then the event is a viable way. If you need to call it more often, then you need to control it by code in all macros itself.
Avatar of Bryce Bassett

ASKER

My checks need to be triggered only one time when the tab is first selected.  

How do I trigger the checks when the tab is first selected?   You say the event is a viable way, but there is no "change tab" event.  So I still do not have an answer to my original question.

The only way I can figure out is to put a line at the beginning of each macro procedure, something like this:  
If startupchecked = false then call startupchecks

Open in new window

1. Determine whether the checks been performed this session.
2. If not, perform the checks, and end by setting a public variable showing the checks have been done.  So they won't be repeated as long as the current session of Word remains open.  

Is there a better approach?
ASKER CERTIFIED SOLUTION
Avatar of ste5an
ste5an
Flag of Germany image

Blurred text
THIS SOLUTION IS 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
Avatar of Bryce Bassett
Bryce Bassett
Flag of United States of America image

ASKER

It works very well!

Thanks for the detail and the code.  I could not have figured it out without your help.  

I will post my sample global template (save in Word STARTUP directory in the AppData area) for others who may be looking for the same solution.
Avatar of Bryce Bassett
Bryce Bassett
Flag of United States of America image

ASKER

Here is my sample global template implementing Ste5an's suggested code.
TriggerTest.dotm
Microsoft Office
Microsoft Office

Microsoft Office is an integrated suite of applications that includes Outlook, Word, Excel, Access, PowerPoint, Visio and InfoPath, along with a number of tools to assist in making the individual components work together. Coding within and between the projects is done in Visual Basic for Applications, known as VBA.

80K
Questions
--
Followers
--
Top Experts
Get a personalized solution from industry experts
Ask the experts
Read over 600 more reviews

TRUSTED BY

IBM logoIntel logoMicrosoft logoUbisoft logoSAP logo
Qualcomm logoCitrix Systems logoWorkday logoErnst & Young logo
High performer badgeUsers love us badge
LinkedIn logoFacebook logoX logoInstagram logoTikTok logoYouTube logo