为特定Excel文件创建菜单项

时间:2017-09-20 11:53:03

标签: excel excel-2010

每当用户打开特定文件时,我都希望在功能区中显示特定的菜单/按钮。这可能吗?

为了清楚说明,当用户打开“ABC.xlsx”时,我想在顶部的功能区中显示一个菜单/按钮,在用户关闭文件之前应该可以看到。

我看到了创建自定义右键菜单的选项。我正在寻找标准菜单中的选项。

1 个答案:

答案 0 :(得分:0)

试试这个

    Private Sub Workbook_Open()
    On Error Resume Next
    Dim MenuBar As CommandBar
    Dim NewMenu As CommandBarPopup
    Dim NewSubMenu As CommandBarPopup
    Dim NewMenuItem As CommandBarButton
    ' Create new item on Worksheet Menu Bar
    Set MenuBar = Application.CommandBars("Worksheet Menu Bar")
    Set NewMenu = MenuBar.Controls.Add(Type:=msoControlPopup)
    NewMenu.Caption = "new menu"

    Set NewMenuItem = NewMenu.Controls.Add(Type:=msoControlButton)
    With NewMenuItem
    .Caption = "My Menu" ' Menu name
    .FaceId = 9
    .OnAction = "Module1.Mymacro" ' call macro here
    End With

    End Sub