rediffusion
Board Regular
- Joined
- Apr 24, 2019
- Messages
- 60
Well... for example, I remained on `Developer` tab. I сlosed the book, started it and with opening `Developer` tab! Is it possible?
Option Explicit
Private Sub Workbook_Open()
Call ActivateRibbonTab(TabName:=[LastActiveRibbonTab])
End Sub
Private Sub Workbook_BeforeClose(Cancel As Boolean)
Names.Add "LastActiveRibbonTab", GetActiveRibbonTab
Me.Save
End Sub
Private Function GetActiveRibbonTab() As String
Const CHILDID_SELF = 0&, NAVDIR_FIRSTCHILD = 7&
Const NAVDIR_LASTCHILD = 8&, NAVDIR_NEXT = 5&
Const STATE_SELECTED = &H300002
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 accObj.accState(0&) = STATE_SELECTED Then
GetActiveRibbonTab = accObj.accName(CHILDID_SELF)
Exit Function
End If
Next j
End If
Next i
End Function
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
Private Sub Workbook_Open()
Call ActivateRibbonTab(TabName:=[LastActiveRibbonTab])
End Sub