Show Different Custom Ribbon Tabs in Word Templates
Question details
Show only the custom Ribbon commands relevant to the active Word document while keeping macros in one DOTM file.
- Product
- Microsoft Word
- Device & OS
- not provided
- Scenario
- A workflow uses two document templates, but both currently share one custom Ribbon tab and one macro template.
- Observed behavior
- The desired result is for each document to display its relevant custom tab while the macros remain in a single global template.
Identify the two sets of commands and decide how each document will be classified, such as with a document variable. You will need access to the global DOTM's Ribbon XML and VBA project, and Word must be allowed to run its macros.
Control both tabs from one global DOTM template
Use Ribbon XML visibility callbacks and a document variable to show the tab associated with the active document.
This example uses a document variable named Workflow with the value A or B. Documents without either value show neither custom tab.
Ribbon visibility is cached, so the code invalidates the Ribbon when the active window changes and when the workflow value is updated.
Open the global DOTM in a Ribbon XML editor. Add onLoad="RibbonLoaded" to the existing customUI element, then set getVisible="ShowTabA" on the first custom tab and getVisible="ShowTabB" on the second. Keep each tab's existing groups and commands. If customUI already has an onLoad callback, merge the RibbonLoaded logic into that callback instead of adding a second onLoad attribute.
In Word, press Alt+F11 and insert a standard module in the global template. Add this code: Public RibbonUI As IRibbonUI Public AppEvents As CAppEvents Public Sub RibbonLoaded(ribbon As IRibbonUI) Set RibbonUI = ribbon End Sub Public Sub AutoExec() Set AppEvents = New CAppEvents Set AppEvents.App = Word.Application End Sub Public Sub ShowTabA(control As IRibbonControl, ByRef returnedVal) returnedVal = (CurrentWorkflow() = "A") End Sub Public Sub ShowTabB(control As IRibbonControl, ByRef returnedVal) returnedVal = (CurrentWorkflow() = "B") End Sub Private Function CurrentWorkflow() As String Dim v As Variable If Documents.Count = 0 Then Exit Function For Each v In ActiveDocument.Variables If v.Name = "Workflow" Then CurrentWorkflow = v.Value Exit Function End If Next v End Function
In the VBA editor, select Insert > Class Module and set its Name property to CAppEvents. Add the following code: Public WithEvents App As Word.Application Private Sub App_WindowActivate(ByVal Doc As Document, ByVal Wn As Window) If Not RibbonUI Is Nothing Then RibbonUI.Invalidate End Sub AutoExec initializes the event handler when the global template loads at Word startup. If you load the template later, run AutoExec manually.
Add this procedure to the standard module, then activate each document and run SetWorkflow "A" or SetWorkflow "B" from the VBA Immediate window. Save the document to retain its variable. Public Sub SetWorkflow(value As String) Dim v As Variable Dim found As Boolean For Each v In ActiveDocument.Variables If v.Name = "Workflow" Then v.Value = value found = True Exit For End If Next v If Not found Then ActiveDocument.Variables.Add Name:="Workflow", Value:=value If Not RibbonUI Is Nothing Then RibbonUI.Invalidate End Sub
Save the DOTM and load it as a global template through Word's Templates and Add-ins settings, or place it in Word's configured Startup folder. Restart Word, enable trusted macros if prompted, and switch between documents marked A and B to check that the matching tab appears.

Use a separate Ribbon template for each workflow
Choose separate DOTM files if keeping all macros in one global file is not essential.
Edit Word documents with WPS Writer
WPS Office is a free, lightweight Office alternative with a familiar interface and Word format compatibility for an easier move between document editors. It does not configure or repair Microsoft Word's custom Ribbon callbacks.

Frequently Asked Questions
Can both custom tabs and all macros stay in one DOTM?
Yes. Put both tabs in the global DOTM's Ribbon XML and use getVisible callbacks to select a tab based on the active document. Keep the callbacks and macros in the same template.
Why does the wrong tab remain visible after I switch documents?
Word caches Ribbon callback results. Use an Application WindowActivate event to call RibbonUI.Invalidate, and also invalidate the Ribbon whenever your code changes the document's workflow value.
What happens if a document has no Workflow variable?
In the example, neither custom tab appears. Activate the document, run SetWorkflow "A" or SetWorkflow "B" in the VBA Immediate window, and save the document.
Can I use the attached template instead of a document variable?
Yes. Your visibility callbacks can inspect ActiveDocument.AttachedTemplate to identify the workflow. Make sure the template names are consistent and invalidate the Ribbon when the active document changes.
Why are the callbacks not running?
Check that the global DOTM is loaded in Word's Templates and Add-ins settings, macros are permitted, and the callback names in the Ribbon XML exactly match the public VBA procedures.




