This is a toggleButton
to switch Chart.ProtectFormatting
on/off.
How do you build it to always “show” the active charts’ state?
If Not TypeName(Selection) = "ChartArea" Then Exit Sub
does not work.
' -- 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="Protect Formatting">
<toggleButton id="ToggleButton4"
label="ChtLock"
imageMso="Lock"
size="large"
getPressed="ToggleButton4_Startup"
onAction="ToggleButton4_OnAction"/>
</group>
</tab>
</tabs>
</ribbon>
</customUI>
' -- Standard Module
Option Explicit
Public RibUI As IRibbonUI
Sub LoadRibbon(Ribbon As IRibbonUI)
Set RibUI = Ribbon
RibUI.InvalidateControl "ToggleButton4"
End Sub
' ToggleButton 4 Startup
Sub ToggleButton4_Startup( _
ByRef control As IRibbonControl, _
ByRef returnedVal)
'If Not TypeName(Selection) = "ChartArea" Then Exit Sub
returnedVal = (ActiveChart.ProtectFormatting) = True
End Sub
' ToggleButton 4 Click
Sub ToggleButton4_OnAction( _
ByRef control As IRibbonControl, _
ByRef pressed As Boolean)
'If Not TypeName(Selection) = "ChartArea" Then Exit Sub
Select Case pressed
Case True
ActiveChart.ProtectFormatting = True
Case False
ActiveChart.ProtectFormatting = False
End Select
End Sub
' -- ThisWorkbook
Private Sub Workbook_SheetActivate(ByVal Sh As Object)
RibUI.InvalidateControl "ToggleButton4"
End Sub
Private Sub Workbook_SheetSelectionChange(ByVal Sh As Object, ByVal Target As Range)
RibUI.InvalidateControl "ToggleButton4"
End Sub