此下拉菜单允许您在三个页面比例值之间进行选择。
除了为每个值构建一个“案例”之外,还有其他方法可以为代码提供从 10 到 400 的整个范围吗?
在我的问题Excel功能区组合框:设置页面比例值(适用于Mac)中,Tim Williams建议尝试:
GetPageScale = ActiveSheet.PageSetup.Zoom & "%"
或
iSize = CLng(Replace(id, "%", ""))
代码中任意一行的正确位置在哪里?
' -- 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
微软文档:
'-- 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