I've been Googling for a user-friendly way to reconnect the Ribbon after an error, mostly (in my limited experience so far) after trying to InvalidateControl().

So far, the recommendations usually involve reloading the workbook or document.

The best workaround I've come up with is not very elegant, and I don't know how bullet-proof it will be in more circumstances.

I was wondering if anyone could offer suggestions to make it more robust?


[VBA]
Option Explicit
Public gobjRibbon As IRibbonUI, gobjRibbonBackup As IRibbonUI
Public gbDetailed As Boolean, gbBrief As Boolean, gbCSheet As Boolean

Public Sub OnRibbonLoad(ribbon As IRibbonUI)
Set gobjRibbon = ribbon
Set gobjRibbonBackup = gobjRibbon
End Sub

'callback for View buttons
Sub ViewPressed(control As IRibbonControl, pressed As Boolean)
Dim bBeenThere As Boolean

bBeenThere = False

On Error GoTo ReinitRibbon
'Set gobjRibbon = Nothing ' for testing purposes
With control
Select Case .id
Case "togDetailed"
Call PBOM_Detailed
gbDetailed = True
gbBrief = False
gbCSheet = False
Case "togBrief"
Call PBOM_Brief
gbDetailed = False
gbBrief = True
gbCSheet = False
Case "togCSheet"
Call PBOM_Csheet
gbDetailed = False
gbBrief = False
gbCSheet = True
End Select
End With

gobjRibbon.InvalidateControl ("togDetailed")
gobjRibbon.InvalidateControl ("togBrief")
gobjRibbon.InvalidateControl ("togCSheet")
bBeenThere = False
Exit Sub
ReinitRibbon:
If Err.Number = 91 And Not bBeenThere And Not gobjRibbonBackup Is Nothing Then
bBeenThere = True
Set gobjRibbon = gobjRibbonBackup
Resume
Else
Call MsgBox("Sorry - There was as problem with the Ribbon object, " & vbCrLf & vbCrLf & _
"so I think you'll have to save and re-load this workbook." & vbCrLf & vbCrLf & _
"But the good news is you probably didn't lose any data", vbCritical + vbOKOnly, sTitle)
Exit Sub
End If
End Sub
[/VBA]

Paul