自定义功能区 \ 从以下位置选择命令:宏 \ 列出死程序

Customize ribbon \ Choose commands from: Macros \ lists dead procedures

我开发了一个 Excel 插件,主要供我个人使用。我在功能区中添加了按钮来触发宏。我觉得我的加载项现在比我刚开始时成熟得多。到了我想与同事共享加载项的地步。

这是我的问题;我希望它能够清楚地传达需要链接到功能区按钮的宏。第一张图片显示了我想要的,以 "CustomAddIn_" 前缀列出的程序。

第二张图片显示了我无法理解的内容...此处列出的过程也不再存在于加载项中(已被注释掉或完全删除)。我看到这些以“.”为前缀。知道这里发生了什么吗?我想从 "Choose commands from \ Macros" 列表框中删除这些。

谢谢!

Screen Grab - Updated

我假设您是在谈论 select 时出现的宏列表: 文件>选项>自定义功能区>宏...

您可以将它们添加到您的加载项代码中,而不是以这种方式添加它们。因此,如果用户打开了加载项,它们只会出现在加载项菜单中。

这也意味着如果您有许多加载项,每个加载项 adds/removes 它都有自己的一组按钮。所以这是我的加载项菜单当前的样子:

这是我用来创建这个按钮的代码。因此,将此代码添加到您的每个加载项文件中。您只需在 Auto_Open 中为您需要创建的每个按钮调用 AddToolbarButton

Option Explicit
'/*libdesc*/Create our toolbar when loaded, remove it when we're closed...

Private Const TOOLBAR_NAME      As String = "{Free text Toolbar Name}"

Sub Auto_Open()
  ' In some instances of Excel, the MacroOptions call provokes an error about "hidden workbooks"
  ' so the on error handler avoid that (the call seems to still work strangely enough)
  On Error Resume Next
  ' faceid 422 is a small graph... looks a bit like curves, see also 418
  ' faceid 107 is a table with a lightning bolt - seems OK ....
  ' faceid 2170 is somethign with colours ...
  ' See http://www.outlookexchange.com/articles/toddwalker/images/icons1-500.jpg
  ' http://www.outlookexchange.com/articles/toddwalker/BuiltInOLKIcons.asp
  AddToolbarButton TOOLBAR_NAME, "{Name of Macro here}", 3865, "Button title that will appear in Toolbar", _
                  "The hoover description (Excel toolbar item text) for this button"

End Sub

Sub Auto_Close()
  On Error Resume Next
  DeleteToolbar TOOLBAR_NAME
End Sub

Function AddToolbarButton(cmdbarname As String, _
                          Optional handler As String = "", _
                          Optional btnIconFace As Integer = -1, _
                          Optional btnCaption As String = "", _
                          Optional btnTip As String = "") As Button
  Dim prevDisp As Boolean: prevDisp = Application.DisplayAlerts
  Dim cmdbar As CommandBar
  Dim btn As CommandBarButton

  Application.DisplayAlerts = False
  On Error Resume Next

  Set cmdbar = Application.CommandBars(cmdbarname)
  If cmdbar Is Nothing Then
    Set cmdbar = Application.CommandBars.Add(cmdbarname)
    If Not cmdbar Is Nothing Then
      cmdbar.Visible = True
      cmdbar.Position = msoBarTop
    End If
  End If
  '/*link*/Icon Codes,http://supportingtech.blogspot.com/2011/03/microsoft-faceid-numbers-for-vba.html
  '/*link*/Excel Icon use by ribbon,http://support2.microsoft.com/default.aspx?scid=kb;[LN];Q213552
  If handler <> "" And Not cmdbar Is Nothing Then
    Set btn = cmdbar.Controls.Add(Type:=msoControlButton)
    If Not btn Is Nothing Then
      btn.OnAction = handler

      If btnIconFace >= 0 Then
        btn.Style = IIf(btnCaption = "", msoButtonIcon, msoButtonIconAndCaption)
        btn.FaceId = btnIconFace
      ElseIf btnCaption <> "" Then
        btn.Style = msoButtonCaption
      End If
      If btnCaption <> "" Then
        btn.Caption = btnCaption
      End If

      btn.DescriptionText = IIf(btnTip = "", handler, btnTip)
      btn.TooltipText = IIf(btnTip = "", handler, btnTip)
    End If
    Set AddToolbarButton = btn
  End If

  Application.DisplayAlerts = prevDisp

End Function

Function DeleteToolbar(cmdbarname As String)
  On Error Resume Next
  Dim cmdbar As CommandBar
  Set cmdbar = Application.CommandBars(cmdbarname)
  If Not cmdbar Is Nothing Then
    cmdbar.Delete
    Set cmdbar = Nothing
  End If
End Function

我刚才问过这个问题,觉得我欠未来的读者更新。我无法重现这个问题,但大卫的回答让我找到了解决方案:

由于错误似乎与加载项文件的某种副本有关(有时是我自己做的),所以我完全重新创建了加载项。我将模块中的所有代码复制到记事本文件中,将模块添加到新的 excel 文件中,复制代码,然后将用户窗体从一个项目拖放到另一个项目。另存为 xlam。那对我有用。