此代码允许您在工作簿中的三个工作表之间进行选择:
我正在尝试对此进行调整,以使用组合框从三种页面尺寸中进行选择。
回调有效。但我无法找出
SaveSetting
事件中相应的 Workbook_SheetActivate
以使组合框在多个工作表上工作并始终显示活动工作表的页面大小。
谢谢!
--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="GroupDemo2"
label="SelectPapersize"
imageMso="AddInManager">
<comboBox id="ComboBox001"
label="comboBox001"
getText="ComboBox001_GetText"
onChange="ComboBox001_OnChange">
<item id="Item_A3"
label="A3"/>
<item id="Item_A4"
label="A4"/>
<item id="Item_A5"
label="A5"/>
</comboBox>
</group>
</tab>
</tabs>
</ribbon>
</customUI>
--Callback VBA in Module "RibbonCallbacks"
Option Explicit
Public RibUI As IRibbonUI
Public Const myApp As String = "RibbApp", mySett As String = "Settings", myVal As String = "Value"
Sub LoadRibbon(Ribbon As IRibbonUI)
Set RibUI = Ribbon
RibUI.InvalidateControl "ComboBox001"
End Sub
'Callback for ComboBox001 onChange
Sub ComboBox001_OnChange(control As IRibbonControl, id As String)
Select Case id
Case "A3"
' ThisWorkbook.Sheets("Sheet1").Select
ActiveSheet.PageSetup.PaperSize = xlPaperA3
Case "A4"
' ThisWorkbook.Sheets("Sheet2").Select
ActiveSheet.PageSetup.PaperSize = xlPaperA4
Case "A5"
' ThisWorkbook.Sheets("Sheet3").Select
ActiveSheet.PageSetup.PaperSize = xlPaperA5
End Select
RibUI.InvalidateControl "ComboBox001"
SaveSetting myApp, mySett, myVal, id
End Sub
'Callback for ComboBox001 getText
Sub ComboBox001_getText(control As IRibbonControl, ByRef returnedVal)
Dim comboVal As String
comboVal = GetSetting(myApp, mySett, myVal, "No Value") 'read it from Registry
If comboVal <> "No Value" Then
returnedVal = comboVal
End If
End Sub
--VBA in "ThisWorkbook"
Private Sub Workbook_Open()
End Sub
Private Sub Workbook_SheetActivate(ByVal Sh As Object)
Dim idx As String
Select Case PaperSize
Case "xlPaperA3"
idx = "A3"
Case "xlPaperA4"
idx = "A4"
Case "xlPaperA5"
idx = "A5"
End Select
SaveSetting myApp, mySett, myVal, idx
RibUI.InvalidateControl "ComboBox001"
End Sub
' -- Stardard module
Option Explicit
Public RibUI As IRibbonUI
Sub LoadRibbon(Ribbon As IRibbonUI)
Set RibUI = Ribbon
RibUI.InvalidateControl "ComboBox001"
End Sub
'Callback for ComboBox001 onChange
Sub ComboBox001_OnChange(control As IRibbonControl, id As String)
Dim iSize As Long
Select Case id
Case "A3"
iSize = xlPaperA3
Case "A4"
iSize = xlPaperA4
Case "A5"
iSize = xlPaperA5
End Select
If iSize > 0 Then _
ActiveSheet.PageSetup.PaperSize = iSize
End Sub
'Callback for ComboBox001 getText
Sub ComboBox001_getText(control As IRibbonControl, ByRef returnedVal)
returnedVal = GetPageSize
End Sub
Function GetPageSize() As String
Select Case ActiveSheet.PageSetup.PaperSize
Case xlPaperA3
GetPageSize = "A3"
Case xlPaperA4
GetPageSize = "A4"
Case xlPaperA5
GetPageSize = "A5"
End Select
End Function
' -- ThisWorkbook
Private Sub Workbook_SheetActivate(ByVal Sh As Object)
RibUI.InvalidateControl "ComboBox001"
End Sub