Reputation: 109
This is a follow-up question to this one:
Excel Ribbon Button toggleButton: Shrink to fit (text) - exclude charts from selection
Using If not TypeName(Selection) = "Range" then Exit Sub
, the button now knows to only work, if the selection is a range. In other words: If the selection is anything else than a range, e.g. a chart, the procedure is stopped.
How do you enable/disable the button to mirror this behavior and change its state depending on the selection?
Will it need the property getEnabled
?
' -- 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="Alignment">
<toggleButton id="ToggleButton1"
label="Shrink to fit"
imageMso="AsianLayoutCharacterScaling"
size="large"
getPressed="ToggleButton1_Startup"
onAction="ToggleButton1_OnAction"/>
</group>
</tab>
</tabs>
</ribbon>
</customUI>
' -- Standard Module
Option Explicit
Public RibUI As IRibbonUI
Sub LoadRibbon(Ribbon As IRibbonUI)
Set RibUI = Ribbon
RibUI.InvalidateControl "ToggleButton1"
End Sub
' ToggleButton 1 Startup
Sub ToggleButton1_Startup( _
ByRef control As IRibbonControl, _
ByRef returnedVal)
If not TypeName(Selection) = "Range" then Exit Sub
returnedVal = (Selection.ShrinkToFit = True)
End Sub
' ToggleButton 1 Click
Sub ToggleButton1_OnAction( _
ByRef control As IRibbonControl, _
ByRef pressed As Boolean)
If not TypeName(Selection) = "Range" then Exit Sub
Select Case pressed
Case True
Selection.ShrinkToFit = True
Case False
Selection.ShrinkToFit = False
End Select
End Sub
' -- ThisWorkbook
Private Sub Workbook_SheetActivate(ByVal Sh As Object)
RibUI.InvalidateControl "ToggleButton1"
End Sub
Private Sub Workbook_SheetSelectionChange(ByVal Sh As Object, ByVal Target As Range)
RibUI.InvalidateControl "ToggleButton1"
End Sub
Upvotes: 0
Views: 45