More XRibbon questions

ClimoC

Well-known Member
Joined
Aug 21, 2009
Messages
584
I'm getting there... slowly.

I have no problem with the XML. That's all fine.

My checkBoxes are linked to 3 cells in a data-store sheet. Click the checkbox, and the value is set in the datasheet to true/false, reflecting the checkbox.

But they always start as False. One of the checkboxes I'd like to default to True

Current Ribbon VB code:

Code:
Public MyRibbon As IRibbonUI

Public Declare Sub CopyMemory Lib "kernel32" Alias _
    "RtlMoveMemory" (destination As Any, source As Any, _
    ByVal length As Long)

Public Sub ribb******ed(ribbon As IRibbonUI)
   ' Store pointer to IRibbonUI
   Dim lngRibPtr As Long
' Store the custom ribbon UI Id in a static variable.
' This is done once during load of UI. I.e. during workbook open.
    Set guiRibbon = ribbon
    lngRibPtr = ObjPtr(ribbon)
    ' Write pointer to worksheet for safe keeping
    Thisworkbook.Sheets("GridData").Range("E10").Value = lngRibPtr
    ShwKeyDates = True
End Sub

Function GetRibbon(lngRibPtr As Long) As Object
   Dim objRibbon As Object
   CopyMemory objRibbon, lngRibPtr, 4
   Set GetRibbon = objRibbon
   Set objRibbon = Nothing
End Function

Public Sub RXReFire(Thisribbon As IRibbonUI)
    Set MyRibbon = Thisribbon
End Sub

Public Sub RefreshRibbon()
If Not (MyRibbon Is Nothing) Then

    MyRibbon.Invalidate
    ribb******ed MyRibbon
Else
Set MyRibbon = GetRibbon(CLng(Thisworkbook.Sheets("GridData").Range("E10").Value))
    MyRibbon.Invalidate
    ribb******ed MyRibbon
End If
End Sub

Public Sub GetPressed(control As IRibbonControl, ByRef pressedState)
        Select Case control.ID
                Case "SeriesInSlotChk"
                    If Thisworkbook.Sheets("GridData").Range("D6").Value = "TRUE" Then
                            pressedState = True
                    End If
                Case "ShowKeyDates"
                    If Thisworkbook.Sheets("GridData").Range("D7").Value = "TRUE" Then
                            pressedState = True
                    End If
                Case "ShwKeyDates"
                    If Thisworkbook.Sheets("GridData").Range("E7").Value = "TRUE" Then
                            pressedState = True
                    End If
                End Select
                
End Sub

Public Sub ClickAction(control As IRibbonControl, pressed As Boolean)
        Select Case control.ID
                Case "SeriesInSlotChk"
                    If pressed = True Then
                            Thisworkbook.Sheets("GridData").Range("E6").Value = "TRUE"
                    ElseIf pressed = False Then
                            Thisworkbook.Sheets("GridData").Range("E6").Value = "FALSE"
                    End If
                Case "ShwKeyDates"
                    If pressed = True Then
                            Thisworkbook.Sheets("GridData").Range("E7").Value = "TRUE"
                    ElseIf pressed = False Then
                            Thisworkbook.Sheets("GridData").Range("E7").Value = "FALSE"
                    End If
                Case "EpNumInSlotChk"
                    If pressed = True Then
                            Thisworkbook.Sheets("GridData").Range("E8").Value = "TRUE"
                    ElseIf pressed = False Then
                            Thisworkbook.Sheets("GridData").Range("E8").Value = "FALSE"
                    End If
        End Select
                
                    
End Sub

So I'd assume that either when the Ribbon is invalidated to update, or at least just once during my workbook_open script, I throw in Call somewhere.

Helpy?

Thanks
C
 

Excel Facts

Can you AutoAverage in Excel?
There is a drop-down next to the AutoSum symbol. Open the drop-down to choose AVERAGE, COUNT, MAX, or MIN
I haven't checked this because I'm not at the right computer, but I think it's as follows.

Your 'GetPressed' sub should be a function and I'm guessing you refer to this in your XML (something like < checkBox ... getPressed="GetPressed" ... / > ). Then rather than trying to change 'pressed state' you return the value to the function (e.g. Case ... GetPressed = FALSE).

The GetPressed Function will be called on loading the Ribbon and every time it's invalidated.

Probably.

Regards
Adam
 
Upvote 0
I haven't checked this because I'm not at the right computer, but I think it's as follows.

Your 'GetPressed' sub should be a function and I'm guessing you refer to this in your XML (something like < checkBox ... getPressed="GetPressed" ... / > ). Then rather than trying to change 'pressed state' you return the value to the function (e.g. Case ... GetPressed = FALSE).

The GetPressed Function will be called on loading the Ribbon and every time it's invalidated.

Probably.

Regards
Adam

In the words of Bartholemew... "Zuh?" (jks)

Essentially I gathered to change the sub to a function, which I've done, and the whole thing continues to work...

"Rather than trying to change 'pressed state' you return the value to the function (e.g. Case ... GetPressed = FALSE). <- This bit I don't quite get... do you mean for the GetPressed Function to evaluate to a boolean? (e.g.
Code:
Public Function GetPressed(control as iRibbonControl, ByRef pressedState) as Boolean

My only thought here is that I can't see or think of where the 'getPressed' callback in the XML is used in the invalidation/validation (or does it check all of that anyway?)

Ta
C
 
Upvote 0
Hehe, sorry, I'm not the master of explanation.

If your XML contains 'getPressed="GetPressed"' then everytime it loads the Ribbon it will check for a GetPressed function within the code to determine what state to dispaly. As an aside it might be worth changing the name of the Function to reduce confusion.

It could be as simple as this
'getPressed="GetCheck1State"'
Code:
Public Function GetCheck1State(control as iRibbonControl, ByRef pressedState) as Boolean
     GetCheck1State = TRUE
End Function

This will set whichever CheckBox you wrote that XML for to TRUE (i.e. checked) every time the Ribbon is loaded. The Ribbon gets loaded everytime you open the sheet/application (obviously) and everytime it gets Invalidated (less obviously lol). You code is more complex but can still follow this basic structure.

Theoretically you can Invalidate (and therefore reload) individual controls on the Ribbon but I've never found a performance advantage for this over just Invalidating the whole thing.

You can use getLabel, getImage and other similar things on other controls in the same manner.

Does this help more? :S

Regards
Adam
 
Upvote 0
So you would recommend I modify the XML to have bespoke getPressed callbacks for each of the checkboxes (getPressed="chkBox1Check".......getPressed="chkBox2Check" [For checkBoxes 1 and 2 etc)....

Then those functions (back in VBA) to simply evaluate to true or false (as you've done in your example above)

The thing I'm not that sure on is, that my Case statement should already be doing this... no? Because the control ID that is passed to the function gets it to check the correct Cell value (TRUE/FALSE) in the data sheet - which is why through all this I can't see why it doesn't already work

Thanks for your help and feedback Adam. Really appreciate it!

C
 
Upvote 0
...or is it just that I need to add

Code:
GetPressed = TRUE

into the current case statement by where it gets the 'pressedState' boolean value?

Narf
 
Upvote 0
...or is it just that I need to add

Code:
GetPressed = TRUE

into the current case statement by where it gets the 'pressedState' boolean value?

Narf

Yes absolutely. Having a single function and the Select Case statement is perfect. It was just the way the code then tried to return the value that was not working. Replace the pressedState but with the GetPressed stuff and it 'should' be rosy ;)

Regards
Adam
 
Upvote 0
"...willing to take a leap of faith? or become an old man, waiting to die, filled with regret?"

Drum roll please. I'm gonna have a ciggie, a pap, then run this through

Ta
C
 
Upvote 0
Nein...

Doesn't set the Checkbox as ticked... Not on-Load and not on re-validation

I have tried removing "pressedState = True" from the Case statement and replacing with "GetPressed = True", and have tried them both together.

One thing I'm going to try quickly, is to change
Code:
If Thisworkbook.Sheets("GridData").Range("E6").Value = "TRUE" Then
GetPressed = True

to

Code:
If Thisworkbook.Sheets("GridData").Range("E6").Value = True Then
GetPressed = True

And see if that works.

Need a few minutes to try it though. Interrupting thd on-Load call gives my Xl2010 the dreaded recurring "Code execution has been interrupted" bug, so need a full restart!
 
Upvote 0

Forum statistics

Threads
1,215,254
Messages
6,123,894
Members
449,132
Latest member
Rosie14

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top