excelvbacomboboxribbonxpage-size

Excel Ribbon comboBox: set paper size for multiple sheets


This code allows you to select between three sheets in a workbook:

Excel Ribbon comboBox: always display current selection

I'm trying to adapt this to use the comboBox to select from three page sizes.

The Callback works. But I can't figure out the corresponding SaveSetting in Workbook_SheetActivate event to have the comboBox work on multiple sheets and always show the active sheets' page size.

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"
'            ThisWorkbook.Sheets("Sheet1").Select
             ActiveSheet.PageSetup.PaperSize = xlPaperA3
        Case "A4"
'            ThisWorkbook.Sheets("Sheet2").Select
            ActiveSheet.PageSetup.PaperSize = xlPaperA4
        Case "A5"
'            ThisWorkbook.Sheets("Sheet3").Select
            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") 'read it from Registry
    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

Solution

  • 
    ' -- Stardard module
    
    Option Explicit
    Public RibUI As IRibbonUI
    
    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)
        Dim iSize As Long
        Select Case id
            Case "A3"
                 iSize = xlPaperA3
            Case "A4"
                iSize = xlPaperA4
            Case "A5"
                iSize = xlPaperA5
        End Select
        If iSize > 0 Then _
            ActiveSheet.PageSetup.PaperSize = iSize
    End Sub
    
    'Callback for ComboBox001 getText
    Sub ComboBox001_getText(control As IRibbonControl, ByRef returnedVal)
        returnedVal = GetPageSize
    End Sub
    
    Function GetPageSize() As String
            Select Case ActiveSheet.PageSetup.PaperSize
            Case xlPaperA3
                GetPageSize = "A3"
            Case xlPaperA4
                GetPageSize = "A4"
            Case xlPaperA5
                GetPageSize = "A5"
            End Select
    End Function
    
    
    ' -- ThisWorkbook
    
    Private Sub Workbook_SheetActivate(ByVal Sh As Object)
        RibUI.InvalidateControl "ComboBox001"
    End Sub