我正在使用适用于 Windows 的 Microsoft Excel 2010。
我已经开发了一个 add-in addin.xlam
,其中包含一个 sub main
。addin.xlam
位于正确的位置,因此可以通过菜单看到和选择Developer -> Add-Ins
。当我打开一个普通的工作簿test.xlsm
,然后按Alt + F11
,我可以看到代码addin.xlam
已加载。
我的目标是在 Excel 的菜单栏中添加一个菜单项,以允许用户启动main
. add-in.xlam
通过点击此链接,我的代码addin.xlam
如下:
Option Explicit
Dim cControl As CommandBarButtonPrivate
Sub Workbook_AddinInstall()
On Error Resume Next 'Just in case
'Delete any existing menu item that may have been left.
Application.CommandBars("Worksheet Menu Bar").Controls("Super Code").Delete
'Add the new menu item and Set a CommandBarButton Variable to it
Set cControl = Application.CommandBars("Worksheet Menu Bar").Controls.Add
'Work with the Variable
With cControl
.Caption = "Super Code"
.Style = msoButtonCaption
.OnAction = "main" 'Macro stored in a Standard Module
End With
On Error GoTo 0
End Sub
Private Sub Workbook_AddinUninstall()
On Error Resume Next 'In case it has already gone.
Application.CommandBars("Worksheet Menu Bar").Controls("Super Code").Delete
On Error GoTo 0
End Sub
这段代码很好地放置在ThisWorkbook
中addin.xlam
,在 中也可见test.xlsm
。但我在菜单栏中看不到任何变化。
有谁知道会发生什么?