Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations SkipVought on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Excel 2007 VBA Ribbon problem

Status
Not open for further replies.

FaneDuru

Technical User
Jul 15, 2002
141
0
16
RO
I use an Excel 2007 addin (xlam) and in order to change some control labels, tips, images its XML code contains an 'onLoad' procedure named "Initializare":
Code:
Public rIBB As IRibbonUI

Sub Initializare(Ribbon As IRibbonUI)
    Set rIBB = Ribbon
End Sub
Most of time it works well except some cases when after VBA errors it looks that it looses the rIBB Set declaration. I mean when it tries to Invalidate controls, rIBB is not recognized and I receive the next error message: "Run-time error '91': Object variable or With block variable not set".
I can change the controls attributes only after addin closing and reopening. I tried to catch the error and reset the variable calling the 'Initializare' procedure. It is called without error but nothing (good) happens...
Here is the code where this error appears:
Code:
Sub Invalidare()
   rIBB.InvalidateControl ("checkbox_mem")
End Sub
I tried to reinitialize the Ribbon with the next code:
Code:
Sub Invalidare()
   On Error Resume Next
   rIBB.InvalidateControl ("checkbox_mem")
   If Err.Number <> 0 Then
       Err.Clear
       Call Initializare(Nothing)
       If Err.Number <> 0 Then Debug.Print "Problem..."
       rIBB.InvalidateControl ("checkbox_mem")
   End If
   On Error GoTo 0
'Initializare' is called without error (nothing in Immediate Window...) but control can not be changed without closing and reopening.
Is there something wrong with my way of calling 'Initializare' or is it something else?
Thanks in advance!
End Sub
 
Another thing:
When it works well the code:
Code:
  Debug.Print rIBB Is Nothing
returns False.
When it does not work well it returns True meaning that riBB became Nothing...
It looks that my way of calling 'Initializare' is unappropriate. Can anybody help on this issue?
 
Solved the problem (based on Rory Archibald via Ron de Bruin sample):
Initializing code becomes:
Code:
Public rIBB As IRibbonUI

Sub Initializare(Ribbon As IRibbonUI)
 Set rIBB = Ribbon
 ThisWorkbook.Sheets("Memorie").Range("B1").Value = ObjPtr(Ribbon)
End Sub
The pointer to the Ribbon Object is initially memorized.
In order to recuperate the handle to the Ribbon the next code has been inserted:
Code:
Public Declare Sub CopyMemory Lib "kernel32" Alias "RtlMoveMemory" _
        (ByRef destination As Any, ByRef source As Any, ByVal length As Long)

Sub RefreshRibbon()
    Set rIBONUL = GetRibbon(ThisWorkbook.Sheets("Memorie").Range("B1").Value)
End Sub
Function GetRibbon(ByVal lRibbonPointer As Long) As Object
    Dim objRibbon As Object
        CopyMemory objRibbon, lRibbonPointer, LenB(lRibbonPointer)
        Set GetRibbon = objRibbon
        Set objRibbon = Nothing
End Function
And code to detect if iRIB is Nothing:
Code:
Sub Invalidare()
   If rIBB Is Nothing Then Call RefreshRibbon
   rIBB.InvalidateControl ("checkbox_mem")
End Sub
Now it is not neccessary any more to close and reopen the file...
 
Of course it is a mistake here:
Code:
Sub RefreshRibbon()
    Set rIBONUL = GetRibbon(ThisWorkbook.Sheets("Memorie").Range("B1").Value)
End Sub
It should be:
Code:
Sub RefreshRibbon()
    Set rIBB = GetRibbon(ThisWorkbook.Sheets("Memorie").Range("B1").Value)
End Sub
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top