Option Explicit
Public Function ActivateRibbonTab(ByVal strTabName As String) As Boolean
Dim UIA As UIAutomationClient.CUIAutomation
Dim UIElement As UIAutomationClient.IUIAutomationElement
Dim Condition As UIAutomationClient.IUIAutomationCondition
Dim ToolsTab As UIAutomationClient.IUIAutomationElement
Dim LegacyIAccessiblePattern As UIAutomationClient.IUIAutomationLegacyIAccessiblePattern
Set UIA = New CUIAutomation
Set UIElement = UIA.ElementFromHandle(ByVal Application.Hwnd)
If Not (UIElement Is Nothing) Then
Set Condition = UIA.CreateAndCondition(UIA.CreatePropertyCondition(UIA_NamePropertyId, ByVal strTabName), _
UIA.CreatePropertyCondition(UIA_ClassNamePropertyId, "NetUIRibbonTab"))
Set ToolsTab = UIElement.FindFirst(TreeScope_Subtree, Condition)
If Not (ToolsTab Is Nothing) Then
Set LegacyIAccessiblePattern = ToolsTab.GetCurrentPattern(UIA_LegacyIAccessiblePatternId)
LegacyIAccessiblePattern.DoDefaultAction
ActivateRibbonTab = True
End If
End If
End Function