This is a follow-up question to this one:
Excel Ribbon comboBox: always highlight selection
The following code allows you to select from three paper sizes using a comboBox.
There is only only issue: the selected item doesn’t stay highlighted.
@OlleSjögren writes, that you need the getSelectedItemIndex
-Callback to make this work.
Question: How do you build the getSelectedItemIndex-Callback?
If I add it to the XML, I get an error message: “the attribute is not declared”.
Thanks!
' -- XML
<customUI xmlns="http://schemas.microsoft.com/office/2009/07/customui" onLoad="LoadRibbon">
<ribbon>
<tabs>
<tab id="Tabv3.1" label="TOOLS" insertAfterMso="TabHome">
<group id="GroupDemo2"
label="SelectPapersize"
imageMso="AddInManager">
<comboBox id="ComboBox001"
label="comboBox001"
getText="ComboBox001_GetText"
onChange="ComboBox001_OnChange">
<item id="Item_A3"
label="A3"/>
<item id="Item_A4"
label="A4"/>
<item id="Item_A5"
label="A5"/>
</comboBox>
</group>
</tab>
</tabs>
</ribbon>
</customUI>
' -- Callback VBA in Module "RibbonCallbacks"
Option Explicit
Public RibUI As IRibbonUI
Public Const myApp As String = "RibbApp", mySett As String = "Settings", myVal As String = "Value"
Sub LoadRibbon(Ribbon As IRibbonUI)
Set RibUI = Ribbon
RibUI.InvalidateControl "ComboBox001"
End Sub
'Callback for ComboBox001 onChange
Sub ComboBox001_OnChange(control As IRibbonControl, id As String)
Select Case id
Case "A3"
ActiveSheet.PageSetup.PaperSize = xlPaperA3
Case "A4"
ActiveSheet.PageSetup.PaperSize = xlPaperA4
Case "A5"
ActiveSheet.PageSetup.PaperSize = xlPaperA5
End Select
RibUI.InvalidateControl "ComboBox001"
SaveSetting myApp, mySett, myVal, id
End Sub
'Callback for ComboBox001 getText
Sub ComboBox001_getText(control As IRibbonControl, ByRef returnedVal)
Dim comboVal As String
comboVal = GetSetting(myApp, mySett, myVal, "No Value")
If comboVal <> "No Value" Then
returnedVal = comboVal
End If
End Sub
' -- VBA in "ThisWorkbook"
Private Sub Workbook_Open()
End Sub
Private Sub Workbook_SheetActivate(ByVal Sh As Object)
Dim idx As String
Select Case PaperSize
Case "xlPaperA3"
idx = "A3"
Case "xlPaperA4"
idx = "A4"
Case "xlPaperA5"
idx = "A5"
End Select
SaveSetting myApp, mySett, myVal, idx
RibUI.InvalidateControl "ComboBox001"
End Sub