这是一个
toggleButton
,用于打开/关闭 Chart.ProtectFormatting
。
如何构建它以始终“显示”活动图表的状态?
If Not TypeName(Selection) = "ChartArea" Then Exit Sub
不起作用。
' -- 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
另一种选择:
Sub ToggleButton4_OnAction(control As IRibbonControl)
If ActiveChart Is Nothing Then
MsgBox "Select a chart.", vbExclamation
Else
On Error Resume Next
With ActiveChart
If .ProtectFormatting = False Then
.ChartTitle.Format.TextFrame2.TextRange. _
Font.Fill.ForeColor.RGB = RGB(0, 0, 0)
.ProtectFormatting = True
Else
.ProtectFormatting = False
.ChartTitle.Format.TextFrame2.TextRange. _
Font.Fill.ForeColor.RGB = RGB(255, 0, 0)
End If
End With
MsgBox ActiveChart.ProtectFormatting, _
Title:="Active Chart" & vbNewLine & "Protect Formatting"
End If
End Sub
它仍然是一个切换按钮。它会给你反馈:
您只需将图表标题绘制为红色即可。