【问题标题】:Is there an alternative to Project's SetCustomUI for Excel?Project 的 SetCustomUI for Excel 是否有替代方案?
【发布时间】:2013-04-29 05:33:01
【问题描述】:

我有一个带有一堆普通 VBA 宏的 Excel 工作表。此文件不断更新并作为 Excel 加载项 (.xlam) 分发。到目前为止,我对它的工作方式非常满意。

现在我想添加一个功能区选项卡和运行其中一些宏的按钮。最初,我很高兴找到this MSDN article,但随后对 Excel(仅 Project)似乎不存在 SetCustomUI 感到沮丧,所以我不能简单地在 Workbook_Open 中使用它。其他 SO 问题证实了这一点,但不提供替代方案。

我的要求:

  • 用户必须像现在一样访问宏 VBA 代码(加载项在 VBE 中显示为项目)。
  • 更新和重新分发插件必须很容易。今天,我给他们发了一个文件来更新。
  • 我想要一个带有一些宏按钮的功能区选项卡。

有什么想法吗?

【问题讨论】:

  • 我很确定您可以将功能区 CustomUI 嵌入到您的 XLAM 文件中,并且在 MS 的某个地方有一个工具可以做到这一点......
  • 我什至没有尝试过这个。我不知道为什么。让我试一试。
  • 几乎成功了。更新后的结果。
  • 噢!我在Sub 中有错字,我忘记了c as IRibbonControl 参数。现在可以了!

标签: excel vba


【解决方案1】:

这是我在 AddIn 文件上使用了一段时间的一些代码。我从别人那里继承了它,但它对我来说一直很好用。

它应该在AddIns 功能区中创建一个新工具栏:

我想我复制了所有相关的代码。如果您有任何问题或遇到任何问题,请告诉我。

Option Explicit
'This module contains functions and subroutines to create Add-in menus

Public Const MenuName As String = "Menu Name"
Public Const APPNAME As String = "&Menu Name"
Private Sub Credit_Inf()

MsgBox "Created by YOUR NAME"

End Sub

Private Sub Auto_Open()

Dim NewMenuItemMacro As String
Dim NewMenuItem As String
Dim XLCommandBar As Integer
Dim NewItem As CommandBarButton
Dim ToolsMenu As CommandBarPopup
Dim NewMenu As CommandBar

NewMenuItemMacro = MenuName
NewMenuItem = APPNAME & "..."
XLCommandBar = 1 'Worksheet Menu Bar


'Delete the current menu if it exists (just in case)
On Error Resume Next
CommandBars(MenuName).Delete
On Error GoTo 0
Set NewMenu = Application.CommandBars.Add(MenuName, msoBarTop)

' .....
NewMenu.Visible = True

' Create a popup control on the bar and set its caption.
Set ToolsMenu = NewMenu.Controls.Add(Type:=msoControlPopup)
ToolsMenu.Caption = "Who built this?"
ToolsMenu.BeginGroup = True
    With ToolsMenu.Controls.Add(Type:=msoControlButton)
      .OnAction = "Credit_Inf"
      .Caption = "Find out who built this"
      .FaceId = 99
      .Style = msoButtonCaption
      .BeginGroup = False
    End With

'##Repeat ToolsMenu.Controls.Add, as necessary



End Sub


Private Sub Auto_Close()

'Delete the current menu if it exists (just in case)
On Error Resume Next
CommandBars(MenuName).Delete
On Error GoTo 0

End Sub

Private Sub EnableMenuItem(sItem As String, bEnable As Boolean)
On Error GoTo Err_EnableMenuItem

Dim NewItem As CommandBarButton
Dim NewMenu As CommandBar

Set NewMenu = Application.CommandBars(MenuName)

Set NewItem = NewMenu.FindControl(Tag:=sItem, recursive:=True)

NewItem.Enabled = bEnable

Err_EnableMenuItem:
    Resume Next

End Sub
Public Function IsWorkbookOpen() As Boolean

IsWorkbookOpen = True

If Application.Workbooks.count = 0 Then
    IsWorkbookOpen = False
End If

End Function

【讨论】:

  • 谢谢,我也试试这个,告诉你进展如何。
【解决方案2】:

您应该能够将功能区 CustomUI 嵌入到您的 XLAM 文件中。我相信微软有一个工具可以帮助解决这个问题(尽管你也可以手动制作和嵌入它)。


来自 OP,这是他们为实现此目的所做的,使用 Office Custom UI Tool

我已尝试过此答案的建议:在原始工作表中使用工具(我使用自定义 UI 编辑器工具)添加选项卡和按钮,然后另存为加载项。这非常适合我想要的。

设置如下:

Module mdlMyActions in MyAddin.xlsm:

 Public Sub HelloWorld(ctl As IRibbonControl)
    MsgBox("Hello, world!")
End Sub

XML 插入到 MyAddin.xlsm:

 <customUI xmlns="http://schemas.microsoft.com/office/2009/07/customui">
  <ribbon startFromScratch="false">
    <tabs>
      <tab id="tabMyTab" label="My Tab">
        <group id="grpMyGroup" label="My Group">
          <button id="btnHelloWorld" label="Hello World"
                  imageMso="HappyFace" size="large" onAction="HelloWorld" />
        </group>
      </tab>
    </tabs>
  </ribbon>
</customUI>

请注意,这是在保存为加载项之前的全部内容。一旦保存为加载项并安装到 Excel 中,加载项就可以完美运行。


【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2011-05-05
    • 1970-01-01
    • 2014-01-29
    • 1970-01-01
    • 2016-04-30
    • 2020-10-07
    • 2012-09-02
    • 2016-08-06
    相关资源
    最近更新 更多