• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 177
  • Last Modified:

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?
0
barges
Asked:
barges
  • 5
  • 4
  • 2
2 Solutions
 
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
 
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
Cloud Class® Course: Amazon Web Services - Basic

Are you thinking about creating an Amazon Web Services account for your business? Not sure where to start? In this course you’ll get an overview of the history of AWS and take a tour of their user interface.

 
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
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

Featured Post

Cloud Class® Course: Ruby Fundamentals

This course will introduce you to Ruby, as well as teach you about classes, methods, variables, data structures, loops, enumerable methods, and finishing touches.

  • 5
  • 4
  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now