Jaafar Tribak
Well-known Member
- Joined
- Dec 5, 2002
- Messages
- 9,796
- Office Version
- 2016
- Platform
- 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.
Function Usage example:
The following test routine should activate te "Data" tab :
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: