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

时间:2017-05-05 15:15:01

标签: excel vba customization ribbon

我开发了一个Excel加载项,主要供我个人使用。我已经在功能区中添加了按钮来激活宏。我觉得我的加载项现在比我刚开始使用时更加成熟。我希望与同事分享加载项。

这是我的问题;我希望它能清楚地传达需要链接到功能区按钮的宏。第一张图片显示了我打算使用" CustomAddIn _"前缀。

第二张图显示了我无法弄清楚的内容......此处列出的程序也不再存在于加载项中(已被注释掉或完全删除)。我看到这些都以'为前缀。有什么想法在这里发生了什么?我想从\ Macros"中删除这些"选择命令。列表框。

谢谢!

Screen Grab - Updated

2 个答案:

答案 0 :(得分:0)

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

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

这也意味着如果你有许多加载项,每个加载项都会添加/删除它自己的一组按钮。这就是我的加载项菜单目前的样子:

enter image description here

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

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

答案 1 :(得分:0)

我回过头来问这个问题,觉得我欠了未来读者的最新消息。我无法复制这个问题,但大卫的回答让我找到了解决方案:

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