Search code examples
excelvbadropdownribbonxpage-setup

Excel Ribbon dropDown: set page scale value from 10 to 400 (for mac)


This dropDown allows you to select between three page scale values.

Is there a way to feed the code with the whole scale range from 10 to 400 other than building a "Case" for every value?

In my question Excel Ribbon comboBox: set page scale value (for mac), Tim Williams suggests to try:

GetPageScale = ActiveSheet.PageSetup.Zoom & "%"

or

iSize = CLng(Replace(id, "%", ""))

Where would be the right place for either line in the code?

' -- 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="GroupDemo3" 
                label="Page Scale"
                imageMso="AddInManager">
                    <dropDown id="DropDown2"
                    sizeString="xxxx"                    
                    onAction="DropDown2_onAction"
                    getSelectedItemIndex="DropDown2_GetSelectedItemIndex"
                    >
                        <item id="Scale_100"
                        label="100%"/>
                        <item id="Scale_77"
                        label="77%"/>
                        <item id="Scale_68"
                        label="68%"/>
                    </dropDown>
                </group>                 
            </tab>
        </tabs>
    </ribbon>
</customUI>


' -- Standard Module

Option Explicit
Public RibUI As IRibbonUI

Sub LoadRibbon(Ribbon As IRibbonUI)
    Set RibUI = Ribbon
    RibUI.InvalidateControl "DropDown2"
End Sub

'Callback for DropDown2 onAction
Sub DropDown2_onAction(control As IRibbonControl, id As String, index As Integer)
    Dim iSize As Long
    Select Case Right(id, 2) ' id
        Case "100%"
             iSize = 100
        Case "77%"
            iSize = 77
        Case "68%"
            iSize = 68
    End Select
    If iSize > 0 Then _
        ActiveSheet.PageSetup.Zoom = iSize
End Sub

'Callback for DropDown2 getSelectedItemIndex
Sub DropDown2_onAction(control As IRibbonControl, id As String, index As Integer)
    Dim iLoc As Long, sZoom As String
    iLoc = InStr(id, "_")
    If iLoc = 0 Then Exit Sub
    sZoom = Mid(id, iLoc + 1)
    If IsNumeric(sZoom) Then ActiveSheet.PageSetup.Zoom = CInt(sZoom)
End Sub

Function GetPageScale() As String
        Select Case ActiveSheet.PageSetup.Zoom
        Case 100
            GetPageScale = 0 ' "100%"
        Case 77
            GetPageScale = 1 ' "77%"
        Case 68
            GetPageScale = 2 ' "68%"
        End Select
End Function


' -- ThisWorkbook

Private Sub Workbook_SheetActivate(ByVal Sh As Object)
    RibUI.InvalidateControl "DropDown2"
End Sub


Solution

  • Microsoft documentation:

    2.2.20 editBox (Edit Box)

    '-- 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="Group6" label="Views">
                <box id="box" boxStyle="horizontal">
                   <labelControl id="label1" label="Zoom "/>
                   <editBox id="EditBox" 
                   sizeString="999" 
                   maxLength="3" 
                   showLabel="false"
                   getText = "EditBox_getText"
                   onChange="EditBox_onChange"               
                    />               
                   <labelControl id="label2" label="%"/>
                 </box>    
            </group>           
          </tab>
        </tabs>
      </ribbon>
    </customUI>
    
    '-- VBA Code
    Option Explicit
    Public RibUI As IRibbonUI
    
    Sub LoadRibbon(Ribbon As IRibbonUI)
        Set RibUI = Ribbon
    End Sub
    
    'Callback for EditBox onChange
    Sub EditBox_onChange(control As IRibbonControl, text As String)
        Dim iZoom As Long
        iZoom = Val(text)
        If iZoom > 400 Or iZoom < 10 Then
            RibUI.InvalidateControl ("EditBox")
        Else
            ActiveSheet.PageSetup.Zoom = iZoom
        End If
    End Sub
    
    'Callback for EditBox getText
    Sub EditBox_getText(control As IRibbonControl, ByRef returnedVal)
        returnedVal = ActiveSheet.PageSetup.Zoom
    End Sub
    
    ' -- ThisWorkbook
    
    Private Sub Workbook_SheetActivate(ByVal Sh As Object)
        RibUI.InvalidateControl ("EditBox")
    End Sub
    
    

    enter image description here