Hide/Show a customized ribbon tab in Word from VBA

I have tried to find a way to hide or show a customized ribbon tab from VBA in Word.
I have tried to do this with the Custom UI Editor and created XML scripts, but I have not been able to use these scripts from VBA.
The only examples I have seen are modifying tabs, groups and controls in the ribbon. And then the controls can run a macro.
I guess I am trying to run XML scripts from VBA. Is that possible?
Or is there another way to do this?
bargesAsked:
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Rgonzo1971Commented:
Hi,

You could use rondebruin's solution
Option Explicit

Dim Rib As IRibbonUI
Public MyTag As String

'Callback for customUI.onLoad
Sub RibbonOnLoad(ribbon As IRibbonUI)
    Set Rib = ribbon
End Sub

Sub GetVisible(control As IRibbonControl, ByRef visible)
    If MyTag = "show" Then
        visible = True
    Else
        If control.Tag Like MyTag Then
            visible = True
        Else
            visible = False
        End If
    End If
End Sub

Sub RefreshRibbon(Tag As String)
    MyTag = Tag
    If Rib Is Nothing Then
        MsgBox "Error, Save/Restart your workbook"
    Else
        Rib.Invalidate
    End If
End Sub

'Note: Do not change the code above


'**********************************************************************************
'Examples to show only the Tab you want with getVisible and tag in the RibbonX.
'**********************************************************************************

Sub DisplayRibbonTab()
'Show only the Tab, Group or Control with the Tag "MyPersonalTab"
    Call RefreshRibbon(Tag:="MyPersonalTab")
End Sub


Sub DisplayRibbonTab_2()
'Show every Tab, Group or Control with every Tag that start with "My"
    Call RefreshRibbon(Tag:="My*")
End Sub

Sub DisplayRibbonTab_3()
'Show every Tab, Group or Control(we use the wildcard "*")
    Call RefreshRibbon(Tag:="*")
End Sub

'Note: in this example every macro above will show you the custom tab.
'If you add more custom tabs this will be different

Sub HideEveryTab()
'Hide every Tab, Group or Control(we use Tag:="")
    Call RefreshRibbon(Tag:="")
End Sub

Open in new window

refer to
https://www.rondebruin.nl/win/s2/win012.htm

Regards
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
Fabrice LambertFabrice LambertCommented:
Hi,

how is your XML ?
There need to be an event handler attached to the getVisible attribute of you custom tab(s).
0
bargesAuthor Commented:
Hi Rgonzo1971,
Your example demands that I create the tab at opening of the Word document.
I already have a customized tab and want to hide/display it from VBA.
Is it not possible to get the id for a customized tab?

Hi Fabrice,
OK. My XML is in the beginning.
How do I put in the event handler?
0
Build an E-Commerce Site with Angular 5

Learn how to build an E-Commerce site with Angular 5, a JavaScript framework used by developers to build web, desktop, and mobile applications.

Rgonzo1971Commented:
does your RibbonTab have an identifying tag?
like here
Sub DisplayRibbonTab()
'Show only the Tab, Group or Control with the Tag "MyPersonalTab"
    Call RefreshRibbon(Tag:="MyPersonalTab")
End Sub

Open in new window

0
Fabrice LambertFabrice LambertCommented:
How do I put in the event handler?
Sample XML:
<customUI xmlns="http://schemas.microsoft.com/office/2009/07/customui">
    <ribbon>
         <tabs>
              <tab id="tab1" getVisible="Ribbon_GetVisible"/>
         </tabs>
    </ribbon>
</customUI>

Open in new window

Notice the "getVisible" attribute.
0
bargesAuthor Commented:
Hi Rgonzo1971,
It has the label "MyPersonalTab". Is that the same thing as tab id?

Hi Fabrice,
Where do I put that code?
How do I know the tab id for my customized tab?
0
Fabrice LambertFabrice LambertCommented:
Where do I put that code?
No where !
The code I provided is to show you that the tab node can have a getVisible attribute wich specifie wich VBA funtion to call to update the visible status of the tab.

How do I know the tab id for my customized tab
id are not pre-defined. Define your own with the id attribute, it can be any value, but it must be unique.
0
bargesAuthor Commented:
Hi,

Ok. Thank you.
I have managed to hide and display the tab now. But I have to create the Tab via the XML script first.

Maybe I am missing something here? I am trying to sort it out. Correct me if I am wrong.
The Office tabs have an id named by Microsoft and is used like idMso="TabView" for the View Tab.
I still would like to get my hands on a customized Tab created manually before the document is opened.

If I have a group of objects, I can loop throu the collection and find the names or something else, but for Word Tabs that seems to be impossible? Where did the object orientation go, and what is the alternative?
My first question remain: I have tried to find a way to hide or show a customized ribbon tab from VBA in Word.
0
Fabrice LambertFabrice LambertCommented:
I also suggest to use something more WYSIWYG than Custom UI Editor, such as:
Ribbon Creator: http://www.ribboncreator2016.de/en/
Ribbon creation wizard: http://arkham46.developpez.com/articles/office/assistant-ruban/
0
bargesAuthor Commented:
Thank you for the suggestion!
I will try that.

Since there is no solution to my question I will close it now.
0
bargesAuthor Commented:
The limitations in what you can do with XML scripts and Office Tabs is frustrating!
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
UI/UX

From novice to tech pro — start learning today.