Callbacks 5: How to Hide and Unhide Tabs

With this page, I would like to present you with my greatest trick - hiding and unhiding tabs. This was especially a challenging one to figure, despite that I found two sites on the Internet related to hiding tabs. The first site gave a sample download but used buttons (not Ribbon buttons) to hide and unhide. I wasn't able to get this to work with Ribbon buttons. The second site used VB code, not VBA. Try as I may, I couldn't write an equivalent VBA code. Hence, I wrote my own working code from scratch. So, here's how you can hide and unhide tabs using checkboxes.

The Goal

My goal was to hide and unhide tabs with checkBoxes. A checked box would mean the tab would show and unchecked would hide it. I also wanted to remember the settings after the application was closed.

Callback Image

In the image, I have three tabs whose visibility I want to toggle. The Mailings and References tabs are two tabs I hardly use. The third tab Table Tools is one I created that combines controls from the two contextual tabs for tables. There are two important pieces to this puzzle. First is refreshing the Ribbon or invalidating the Ribbon, which allows us to toggle the visibility. The second is the getVisible callback. We'll see how these two work to toggle visibility. Let's begin with the XML code.

Writing the XML Code

You can place the group of checkBoxes in any tab of your Ribbon except the tabs you will be hiding. I placed mine in the Edit and Styles tab. You can also use the tabs of your choice to hide and unhide with my example. Let's add the getVisible callback to the tabs you'll be hiding and create the XML code for the checkBoxes.

<tab idMso="TabMailings" getVisible="GetVisibleMailings" />
<tab idMso="TabReferences" getVisible="GetVisibleReferences" />
<tab id="TableTools" label="Table Tools" getVisible="GetVisibleTableTools">
	<group id="NewTableGroup" label="Table" >
		<gallery idMso="TableInsertGallery" size="large" />
		<gallery idMso="ShadingColorPicker" size="large" />
		<splitButton idMso="TableBordersMenu" size="large" />
	</group>
	<group idMso="GroupTableRowsAndColumns" />
	<group id="NewDrawBorders" label="Borders">
		<dropDown idMso="TableDrawBorderPenStyle" label="Pen Style" showLabel="false" />
		<dropDown idMso="TableDrawBorderPenWeight" label="Pen Weight" showLabel="false" />
		<gallery idMso="BorderColorPicker" label="Pen Color"/>
	</group>
	<group idMso="GroupTableAlignment" />
	<group idMso="GroupTableMerge" />
	<group idMso="GroupTableCellSize" />
	<group idMso="GroupTable" />
</tab>
<tab id="Edit and Styles" />
	<group id="Visibility" label="Show Tabs">
		<checkBox id="VisibleMailings" enabled="true" getPressed="GetPressed" label="Mailings" screentip="Show or Hide Mailings Tab" onAction="ToggleVisibility" />
		<checkBox id="VisibleReferences" enabled="true" getPressed="GetPressed" label="References" screentip="Show or Hide References Tab" onAction="ToggleVisibility" />
		<checkBox id="VisibleTableTools" enabled="true" getPressed="GetPressed" label="Table Tools" screentip="Show or Hide Table Tools Tab" onAction="ToggleVisibility" />
	</group>
</tab>

The Mailings and References tabs are built-in tabs and the Table Tools tab is a custom tab. I added the unique callbacks getVisible="GetVisibleMailings", getVisible="GetVisibleReferences", and getVisible="GetVisibleTableTools" to the respective code for tabs. I wasn't able to use the Select Case statement here, but giving the callbacks unique names allowed me to toggle the visibility individually for the tabs.

The key callback players will be getVisibility for the three tabs, getPressed="GetPressed", and onAction="ToggleVisibility". Let's start writing the procedures for these callbacks.

Writing the Procedures

First, let's declare the variables and write the onLoad procedure.

Public myRibbon As IRibbonUI
Public VisibleMailings As String
Public VisibleReferences As String
Public VisibleTableTools As String
Dim returnedVal As Boolean

Sub OnLoad(ribbon As IRibbonUI) Set myRibbon = ribbon VisibleTableTools = True End Sub

I have declared the myRibbon variable that's important for refreshing the Ribbon (this was discussed earlier). Now, the three variables VisibleMailings, VisibileReferences, and VisibleTableTools will hold either True or False. On refreshing the Ribbon, this variable will determine if the tab is to be visible or hidden based on their value after refresh. The variable returnedVal is a reference variable (ByRef variable) that gets passed on to the three variables. Since this variable is declared as boolean, the other three variables will also take on boolean values despite declaring them as strings.

Now in the OnLoad procedure, I have set VisibleTableTools variable to True so that at launch, this tab will be displayed. You can use your own preference here for your tab variable.

Note: By default, the tabs will be hidden at launch unless you set the visibility variable to equal true. The best way to set the variable is in the OnLoad procedure. For example, to make Table Tools visible, include the line VisibleTableTools = True in the OnLoad procedure.

The next procedure is the one that refreshes the ribbon.

Sub RefreshRibbon()
    myRibbon.Invalidate
End Sub

The next three procedures are the ones that determine if the tabs are to be visible or hidden when the Ribbon refreshes. These three procedures are executed automatically after the Ribbon refreshes.

Sub GetVisibleMailings(control As IRibbonControl, ByRef returnedVal)
    returnedVal = VisibleMailings
End Sub

Sub GetVisibleReferences(control As IRibbonControl, ByRef returnedVal) returnedVal = VisibleReferences End Sub
Sub GetVisibleTableTools(control As IRibbonControl, ByRef returnedVal) returnedVal = VisibleTableTools End Sub

When the Ribbon refreshes, we also need to determine if the checkBoxes are checked or not. The following procedure ensures that the correct state of the checkBox is displayed after refresh.

Sub GetPressed(control As IRibbonControl, ByRef returnedVal)
    Select Case control.ID
        Case "VisibleMailings"
            returnedVal = VisibleMailings
        Case "VisibleReferences"
            returnedVal = VisibleReferences
        Case "VisibleTableTools"
            returnedVal = VisibleTableTools
    End Select
End Sub

Notice that in the GetVisible callbacks and GetPressed callbacks, we have used the same three variables VisibleMailings, VisibleReferences, and VisibleTableTools. These three variables are key players in determining the visibility and checkBoxes state and they need to be toggled when checking or unchecking the checkBoxes. Hence, the final piece is the ToggleVisibility procedure that actually toggles these variables.

Sub ToggleVisibility(control As IRibbonControl, pressed As Boolean)
    Select Case control.ID
        Case "VisibleMailings"
            If pressed = True Then
                VisibleMailings = True
                Call RefreshRibbon
            End If
            If pressed = False Then
                VisibleMailings = False
                Call RefreshRibbon
            End If
        Case "VisibleReferences"
            If pressed = True Then
                VisibleReferences = True
                Call RefreshRibbon
            End If
            If pressed = False Then
                VisibleReferences = False
                Call RefreshRibbon
            End If
        Case "VisibleTableTools"
            If pressed = True Then
                VisibleTableTools = True
                Call RefreshRibbon
            End If
            If pressed = False Then
                VisibleTableTools = False
                Call RefreshRibbon
            End If
    End Select
End Sub

Each time we change the state of the checkBox, we call the RefreshRibbon procedure which invalidates the Ribbon and refreshes it. Then, the getVisible and getPressed callbacks are called to determine the visibility of the tabs and the state of the checkBoxes.

With that, I conclude how to hide and show tabs in the ribbon with checkBox controls. I haven't shown you how to remember the preferences on this page. This is a matter of another topic as it involves writing to INI files to remember the settings.