前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >自定义功能区示例:自定义文件菜单、上下文菜单、功能区选项卡

自定义功能区示例:自定义文件菜单、上下文菜单、功能区选项卡

作者头像
fanjy
发布2023-11-23 14:22:00
2470
发布2023-11-23 14:22:00
举报
文章被收录于专栏:完美Excel完美Excel

标签:VBA,自定义功能区

在《自定义功能区示例:创建用于工作表导航的下拉列表》、《自定义功能区示例:创建用于工作表导航的动态组合框》中,我们在Excel功能区中添加一个自定义的选项卡,然后再该选项卡中添加带有下拉列表或组合框的一个自定义组,可用于从下拉列表中选择工作表,从而快速导航到该工作表,这对于工作簿中有大量工作表且要快速找到相应的工作表的用户来说,非常有用。

下面介绍一个综合示例,来源于forum.ozgrid.com,分别在工作簿文件菜单、右键上下文菜单中添加了自定义命令,也自定义了一个选项卡。可以作为自定义功能区的模板参考。

该工作簿名称为Ribbon and Backstage and Context Menus.xlsm,使用Custom UI Editor for Microsoft Office打开该工作簿,在其中输入代码:

代码语言:javascript
复制
<customUI xmlns="http://schemas.microsoft.com/office/2009/07/customui">
  <ribbon>
    <tabs>
      <tab id="highlightTab"
         label="Unit Assembly Menu" 
         insertBeforeQ="TabFormat">
         <group id="testGroup"
           label="Test">
           <button id="highlightManualTasks"
             label="Toggle Manual Task Color"
             imageMso="DiagramTargetInsertClassic"
             onAction="ToggleManualTasksColor"/>
         </group>
      </tab>
   </tabs>
</ribbon>

<backstage>
 <tab
   id="customTab"
   label="Assembly Units">
   <firstColumn>
   <group
     id="customGroup"
     label="Assembly Units">
     <topItems>
       <button
         id="customButton"
         label="Load &amp;&amp; Return"
         imageMso="BevelShapeGallery"
         onAction="OnAction"
         isDefinitive="true" />
     </topItems>
   </group>
   </firstColumn>
 </tab>

 </backstage>
   <contextMenus>
     <contextMenu idMso="ContextMenuCell">
       <dynamicMenu
         id="MyDynamicMenu"
         label= "My Assembly Unit Menu" 
         imageMso="HappyFace"
         getContent="GetContent"
         insertBeforeMso="Cut"/>
       <menuSeparator id="MySeparator" insertBeforeMso="Cut" />
     </contextMenu>
  </contextMenus>
</customUI>

验证无误后,保存并关闭Custom UI Editor for Microsoft Office。

在Excel中打开Ribbon and Backstage and Context Menus.xlsm,打开VBE,插入一个标准模块,输入下面的代码:

代码语言:javascript
复制
Sub OnAction(control As IRibbonControl)
 MsgBox "Assembly Units"
End Sub
Sub GetContent(control As IRibbonControl, ByRef returnedVal)
 Dim xml As String
 xml = "<menu xmlns=""http://schemas.microsoft.com/office/2009/07/customui"">" & _
       "<button id=""but1"" imageMso=""Help"" label=""Help"" onAction=""HelpMacro""/>" & _
       "<button id=""but2"" imageMso=""FindDialog"" label=""Find"" onAction=""FindMacro""/>" & _
       "</menu>"
 returnedVal = xml
End Sub
Sub HelpMacro(control As IRibbonControl)
 MsgBox "Help macro"
End Sub
Sub FindMacro(control As IRibbonControl)
 MsgBox "Find macro"
End Sub
Sub ToggleManualTasksColor(control As IRibbonControl)
  MsgBox "Assembly Units"
End Sub

保存并关闭该工作簿,然后重新打开该工作簿,即可以看到更新后的自定义功能区界面,如下图所示。

图1

图2

图3

本文参与 腾讯云自媒体同步曝光计划,分享自微信公众号。
原始发表:2023-11-22,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 完美Excel 微信公众号,前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体同步曝光计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档