excelvbatogglebuttonribbonx

Excel Ribbon toggleButton: Page Orientation Landscape - always show current sheets' state


This is a toggleButton to switch between landscape and portrait.

How do you build it to always "show" the active sheets' state?

The getSelectedItemIndex-Callback is not available for toggleButton:

https://learn.microsoft.com/en-us/openspecs/office_standards/ms-customui/ec42bfd0-149c-495b-895c-3bc708b8a149

' -- 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">
          <toggleButton id="ToggleButton01" 
                        label="Orientation" 
                        imageMso="PageOrientationLandscape" 
                        size="large"
                        getPressed="ToggleButton01_Startup" 
                        onAction="ToggleButton01_OnAction"/>       
        </group>           
      </tab>
    </tabs>
  </ribbon>
</customUI>


' -- Standard Module

Option Explicit
Public RibUI As IRibbonUI

Sub LoadRibbon(Ribbon As IRibbonUI)
Set RibUI = Ribbon

End Sub

' ToggleButton 01 Startup
Sub ToggleButton01_Startup( _
   ByRef control As IRibbonControl, _
   ByRef returnedVal)
   If ActiveSheet.PageSetup.Orientation = xlLandscape Then
        returnedVal = xlLandscape
   Else
        returnedVal = xlPortrait
   End If   
End Sub

' ToggleButton 01 Click
Sub ToggleButton01_OnAction( _
   ByRef control As IRibbonControl, _
   ByRef pressed As Boolean)
   Select Case pressed
      Case True
            ActiveSheet.PageSetup.Orientation = xlLandscape
      Case False
            ActiveSheet.PageSetup.Orientation = xlPortrait
   End Select
End Sub

' -- ThisWorkbook

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

Solution

  • ' ToggleButton 01 Startup
    Sub ToggleButton01_Startup( _
       ByRef control As IRibbonControl, _
       ByRef returnedVal)
       returnedVal = (ActiveSheet.PageSetup.Orientation = xlLandscape)
       'If ActiveSheet.PageSetup.Orientation = xlLandscape Then
       '     returnedVal = True ' xlLandscape
       'Else
       '     returnedVal = False 'xlPortrait
       'End If
    End Sub