Activate Ribbon-Tab Without XML or API calls

Jaafar Tribak

Well-known Member
Joined
Dec 5, 2002
Messages
9,796
Office Version
  1. 2016
Platform
  1. Windows
Hi dear members,

I just thought I would share with you here this very small and simple vba function that I recently wrote for switching ribbon tabs.

Code:
Option Explicit

Function ActivateRibbonTab(ByVal TabName As String) As Boolean

    Const CHILDID_SELF = 0&, NAVDIR_FIRSTCHILD = 7&, NAVDIR_NEXT = 5&
    Dim accObj As IAccessible, i As Long, lTabsCount As Long
     
    Set accObj = Application.CommandBars("Ribbon")
    For i = 1 To 7
        Set accObj = accObj.accNavigate(NAVDIR_FIRSTCHILD, CHILDID_SELF)
    Next i
    For i = 1 To 6
        Set accObj = accObj.accNavigate(NAVDIR_NEXT, CHILDID_SELF)
    Next i
    Set accObj = accObj.accNavigate(NAVDIR_FIRSTCHILD, CHILDID_SELF)
    lTabsCount = accObj.accChildCount
    Set accObj = accObj.accNavigate(NAVDIR_FIRSTCHILD, CHILDID_SELF)
    On Error Resume Next
    For i = 1 To lTabsCount
        Set accObj = accObj.accNavigate(NAVDIR_NEXT, CHILDID_SELF) '!!!!
        If UCase(accObj.accName(CHILDID_SELF)) = UCase(TabName) Then
            accObj.accDoDefaultAction CHILDID_SELF
            ActivateRibbonTab = Not CBool(Err.Number)
            Exit Function
        End If
    Next i
End Function

Function Usage example:
The following test routine should activate te "Data" tab :
Code:
Sub Test()
    If ActivateRibbonTab(TabName:="Data") Then
        MsgBox "Ribbon-Tab activated successfully !"
    Else
        MsgBox "Ribbon-Tab not found !"
    End If
End Sub
 
Last edited:

Excel Facts

Return population for a City
If you have a list of cities in A2:A100, use Data, Geography. Then =A2.Population and copy down.
Hi, Jaafar

I was looking for something like your post. I have not found a similar post elsewhere.

I want to activate any chosen tab in Excel (e.g. Data Tab), and I think your code could solve my problem.

But when I ran your code, it blocks at this line:

Code:
  Next i
    Set accObj = accObj.accNavigate(NAVDIR_FIRSTCHILD, CHILDID_SELF)    <----

Any idea what could have gone wrong? I use Excel 2016


-----
What I did:

I simply copied your codes in a VBA Module.

Then I dragged a rectangle shape on my Excel Sheet.

Then assigned the macro "Test" . I use Excel 2016.

Thanks
Leon
 
Last edited:
Upvote 0
@leonlai

Better late than never.

For excel 2016

Code:
Private Function ActivateRibbonTab(ByVal TabName As String) As Boolean

    Const CHILDID_SELF = 0&, NAVDIR_FIRSTCHILD = 7&
    Const NAVDIR_LASTCHILD = 8&, NAVDIR_NEXT = 5&
    Dim accObj As IAccessible, i As Long, j As Long, lChildCount As Long
    
    
    Set accObj = Application.CommandBars("Ribbon")
    
    Set accObj = accObj.accNavigate(NAVDIR_LASTCHILD, CHILDID_SELF)
    Set accObj = accObj.accNavigate(NAVDIR_LASTCHILD, CHILDID_SELF)
    Set accObj = accObj.accNavigate(NAVDIR_LASTCHILD, CHILDID_SELF)
    Set accObj = accObj.accNavigate(NAVDIR_LASTCHILD, CHILDID_SELF)
    Set accObj = accObj.accNavigate(NAVDIR_LASTCHILD, CHILDID_SELF)
    Set accObj = accObj.accNavigate(NAVDIR_FIRSTCHILD, CHILDID_SELF)
    lChildCount = accObj.accChildCount
    Set accObj = accObj.accNavigate(NAVDIR_FIRSTCHILD, CHILDID_SELF)
    
    On Error Resume Next
    For i = 1 To lChildCount
        Set accObj = accObj.accNavigate(NAVDIR_NEXT, CHILDID_SELF)
        If UCase(accObj.accName(CHILDID_SELF)) = UCase("Ribbon Tabs") Then
            Set accObj = accObj.accNavigate(NAVDIR_FIRSTCHILD, CHILDID_SELF)
            lChildCount = accObj.accChildCount
            Set accObj = accObj.accNavigate(NAVDIR_FIRSTCHILD, CHILDID_SELF)
            For j = 1 To lChildCount
                Set accObj = accObj.accNavigate(NAVDIR_NEXT, CHILDID_SELF)
                If UCase(accObj.accName(CHILDID_SELF)) = UCase(TabName) Then
                    accObj.accDoDefaultAction CHILDID_SELF
                    ActivateRibbonTab = Not CBool(Err.Number)
                    Exit Function
                End If
            Next j
        End If
    Next i


End Function


Code Usage:
Code:
Sub Test()
    If ActivateRibbonTab(TabName:="Data") Then
        MsgBox "Ribbon-Tab activated successfully !"
    Else
        MsgBox "Ribbon-Tab not found !"
    End If
End Sub
 
Last edited:
Upvote 0

Forum statistics

Threads
1,223,711
Messages
6,174,028
Members
452,542
Latest member
Bricklin

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top