您现在的位置: > 首页 > 软件应用 > 工具软件
>> 最新教程
>> 热门教程
>> 最新游戏资讯
>> 热门游戏资讯
在Excel 2007中制作个性化选项卡
作者:本站                来自:安迪教程网                 加入时间:08-10-08                进入论坛讨论

  图1展示了一个带有四组控件的自定义Ribbon选项卡(My Stuff)。本节将简要地介绍其RibbonX代码和VBA回调过程。

  hp.agwcn.com

  图1:一个带有四组控件的新的Ribbon选项卡

  1、创建一个新选项卡

  创建新选项卡的RibbonX代码为:

<ribbon>
 <tabs>
  <tab id=”CustomTab” label=”My Stuff”>
 </tabs>
</ribbon>

  技巧:如果想创建一个最小化UI,则Ribbon标记有一个startFromScratch属性。如果设置为True,则将隐藏所有内置选项卡。此外,除了“新建”、“打开”、“Excel选项”和“退出”外,所有Office按钮菜单都会被隐藏。

  2、创建一个Ribbon组

  本示例中的代码,在My Stuff选项卡中创建了四个组。下面是相应的代码:

<group id=”Group1″ label=”Stuff”>
</group>

<group id=”Group2″ label=”More Stuff”>
</group>

<group id=”Group3″ label=”Built In Stuff”>
</group>

<group id=”Group4″ label=”Galleries”>
</group>

  这些成对的<group>和</group>标记位于创建新选项卡的<tab>和</tab>标记里面。

  3、创建控件

  下面是创建第一组中控件的RibbonX代码,如图7所示。注意,这些控件被定义在第一组 标记里面。

  hp.agwcn.com

  图2:带有四个控件的Ribbon组

<group id=”Group1″ label=”Stuff”>
  <labelControl id=”Label1″ getLabel=”getLabel1″ />
  <labelControl id=”Label2″ getLabel=”getLabel2″ />

  <editBox id=”EditBox1″
    showLabel=”true”
    label=”Number:”
    onChange=”EditBox1_Change”/>

  <button id=”Button1″
    label=”Calculator”
    size=”large”
    onAction=”ShowCalculator”
    imageMso=”Calculator” />
</group>

  两个标签控件都有一个相关的VBA回调过程(名为getLabel1和getLabel2),这些过程如下:

Sub getLabel1(control As IRibbonControl, ByRef returnedVal)
  returnedVal = “Hello ” & Application.UserName
End Sub

Sub getLabel2(control As IRibbonControl, ByRef returnedVal)
  returnedVal = “Today is ” & Date
End Sub
当RibbonX代码被装载时,将执行这两个过程,标签控件的标题将随用户名和日期而动态更新。

  编辑框控件有一个名为EditBox1_Change的onChange回调过程,显示输入数字的平方根(或者在不能计算平方根时显示一条错误消息)。EditBox1_Change过程为:

Sub EditBox1_Change(control As IRibbonControl, text As String)
  Dim squareRoot As Double
  On Error Resume Next
  squareRoot = Sqr(text)
  If Err.Number = 0 Then
    MsgBox text & “的平方根是:” & squareRoot
  Else
    MsgBox “输入了一个负数.”, vbCritical
  End If
End Sub

  在Stuff组中的最后一个控件是一个简单的按钮,是执行名为ShowCalculator的VBA过程的参数onAction。使用VBA Shell函数来显示Windows计算器:

Sub ShowCalculator(control As IRibbonControl)
  On Error Resume Next
  Shell “calc.exe”, vbNormalFocus
  If Err.Number <> 0 Then MsgBox “不能开启calc.exe”
End Sub

  图3展示了第二组中的控件,标记为More Stuff。

  hp.agwcn.com

  图3:在这个自定义的Ribbon组中有三个控件

  第二组的RibbonX代码如下:

<group id=”Group2″ label=”More Stuff”>
 <toggleButton id=”ToggleButton1″
   size=”large”
   imageMso=”FileManageMenu”
   label=”Toggle Me”
   onAction=”ToggleButton1_Click” /> <separator id=”sep1″ />  <checkBox id=”Checkbox1″
   label=”Checkbox”
   onAction=”Checkbox1_Change”/>

 <comboBox id=”Combo1″
  label=”Month”
  onChange=”Combo1_Change”>
  <item id=”Month1″ label=”January” />
  <item id=”Month2″ label=”February”/>
  <item id=”Month3″ label=”March”/>
  <item id=”Month4″ label=”April”/>
  <item id=”Month5″ label=”May”/>
  <item id=”Month6″ label=”June”/>
  <item id=”Month7″ label=”July”/>
  <item id=”Month8″ label=”August”/>
  <item id=”Month9″ label=”September”/>
  <item id=”Month10″ label=”October”/>
  <item id=”Month11″ label=”November”/>
  <item id=”Month12″ label=”December”/>
 </comboBox>
</group>
该组包含一个切换按钮、一个分隔条、一个复选框和一个组合框控件。这些控件是相当简单的。除了分隔条控件(插入一条垂直线)外,每个控件都有一个相关的回调过程,简单地显示该控件的状态:

Sub ToggleButton1_Click(control As IRibbonControl, ByRef returnedVal)
  MsgBox “Toggle value: ” & returnedVal
End Sub

Sub Checkbox1_Change(control As IRibbonControl, pressed As Boolean)
  MsgBox “Checkbox value: ” & pressed
End Sub Sub Combo1_Change(control As IRibbonControl, text As String)
  MsgBox text
End Sub

  注:复合框控件也接受用户输入的文本。如果您想限制用户只选择所提供的数据,则使用下拉控件。

  第三组中的控件由内置的控件组成(如图9所示)。要在自定义组中包括内置控件,只需要知道它的名字(参数idMso)。

  hp.agwcn.com

  图4:包含内置控件的组

  其RibbonX代码为:

<group id=”Group3″ label=”Built In Stuff”>
  <control idMso=”Copy” label=”Copy” />
  <control idMso=”Paste” label=”Paste” enabled=”true” />
  <control idMso=”WindowSwitchWindowsMenuExcel”
    label=”Switch Window” />
  <control idMso=”Italic” />
  <control idMso=”Bold” />
  <control idMso=”FileOpen” />
</group>

  这些控件没有回调过程,因为它们执行标准的操作。

  图5展示了最后一组控件,由两个库组成。

  hp.agwcn.com

  图5:该Ribbon组包含两个库

  这两个库控件的RibbonX代码为:

<group id=”Group4″ label=”Galleries”>
 <gallery id=”Gallery1″
   imageMso=”ViewAppointmentInCalendar”
   label=”Pick a Month:”
   columns=”2″ rows=”6″
   onAction=”MonthSelected” >
  <item id=”January” label=”January”
   imageMso=”QuerySelectQueryType”/>
  <item id=”February” label=”February”
   imageMso=”QuerySelectQueryType”/>
  <item id=”March” label=”March”
   imageMso=”QuerySelectQueryType”/>
  <item id=”April” label=”April”
   imageMso=”QuerySelectQueryType”/>
  <item id=”May” label=”May”
   imageMso=”QuerySelectQueryType”/>
  <item id=”June” label=”June”
   imageMso=”QuerySelectQueryType”/>
  <item id=”July” label=”July”
   imageMso=”QuerySelectQueryType”/>
  <item id=”August” label=”August”
   imageMso=”QuerySelectQueryType”/>
  <item id=”September” label=”September”
   imageMso=”QuerySelectQueryType”/>
  <item id=”October” label=”October”
   imageMso=”QuerySelectQueryType”/>
  <item id=”November” label=”November”
   imageMso=”QuerySelectQueryType”/>
  <item id=”December” label=”December”
   imageMso=”QuerySelectQueryType”/>
  <button id=”Today”
    label=”Today…”
    imageMso=”ViewAppointmentInCalendar”
    onAction=”ShowToday”/>
 </gallery>

 <gallery id=”Gallery2″
   label=”Banjo Players”
   size=”large”
   columns=”4″
   itemWidth=”100″ itemHeight=”125″
   imageMso= “Camera”
   onAction=”OnAction”>
   <item id=”bp01″ image=”bp01″ />
   <item id=”bp02″ image=”bp02″ />
   <item id=”bp03″ image=”bp03″ />
   <item id=”bp04″ image=”bp04″ />
   <item id=”bp05″ image=”bp05″ />
   <item id=”bp06″ image=”bp06″ />
   <item id=”bp07″ image=”bp07″ />
   <item id=”bp08″ image=”bp08″ />
   <item id=”bp09″ image=”bp09″ />
   <item id=”bp10″ image=”bp10″ />
   <item id=”bp11″ image=”bp11″ />
   <item id=”bp12″ image=”bp12″ />
   <item id=”bp13″ image=”bp13″ />
   <item id=”bp14″ image=”bp14″ />
   <item id=”bp15″ image=”bp15″ />
 </gallery>
</group>

  图6展示了第一个库,是一个在两列中月份名称的列表。参数onAction执行MonthSelected回调过程,显示所选的月份(存储在参数id):

  hp.agwcn.com

  图6:显示月份名称的库,加上按钮

Sub MonthSelected(control As IRibbonControl, _
  id As String, index As Integer)
  MsgBox “You selected ” & id
End Sub
“Pick a Month”库在底部也包含一个带有自已的回调过程的按钮控件(标记为Today):
Sub ShowToday(control As IRibbonControl)
  MsgBox “Today is ” & Date
End Sub

  第二个库中显示15张照片(您可以将照片添加到相应的文件夹中后,则会显示在库中)。

  这些照片存储在工作簿文件中,在customUI文件夹里名为images的文件夹内。添加图像也需要_rels文件夹,使用相关关系列表。要查看这是如何工作的,添加.zip扩展名到工作簿中,并检验其内容。

联系方式:QQ:6084884 email:agwcn@126.com 粤ICP备05055782号
本网站中发布的文章只代表发表人的个人观点,不代表安迪教程网网站的立场
copyright© 2000-2004 安迪教程网 All rights reserved agwcn.com