在vb.net中将子菜单添加到Excel中的单元格上下文菜单

时间:2019-07-10 14:07:27

标签: excel vb.net excel-addins

我在vb.net中为excel制作了一个插件 我想在单元格上下文菜单中添加一些快捷方式。我设法在此处找到添加按钮的解决方案 https://social.msdn.microsoft.com/Forums/en-US/ae7a6cdd-db2c-4edd-a62a-ac35a466ae5c/how-to-assign-a-method-to-a-commandbarbutton-in-a-cell-contextmenu-in-an-vsto-application-addin-for?forum=vsto

但是我无法添加一个子菜单并将这些按钮放进去

这是我的实际代码 我设法将子菜单和按钮分开了,但是没有将子菜单中的按钮分开了

Private WithEvents buttonVL03N As CommandBarButton
    Private WithEvents buttonIW53 As CommandBarButton
    Private Sub ThisAddIn_Startup() Handles Me.Startup
        Dim rcCellContextMenu As CommandBar = Globals.ThisAddIn.Application.CommandBars("Cell")
        Dim myMenu As CommandBarPopup

        myMenu = TryCast(rcCellContextMenu.Controls.Add(MsoControlType.msoControlPopup, Before:=3), CommandBarPopup)
        myMenu.Caption = "SAP Transactions"
        myMenu.Tag = "SAP shortcuts "
        buttonVL03N = TryCast(rcCellContextMenu.Controls.Add(MsoControlType.msoControlButton, Id:=1, Before:=3, Temporary:=True), CommandBarButton)
        buttonIW53 = TryCast(rcCellContextMenu.Controls.Add(MsoControlType.msoControlButton, Id:=1, Before:=4, Temporary:=True), CommandBarButton)

        If buttonVL03N IsNot Nothing Then
            buttonVL03N.Caption = "VL03N"
            buttonVL03N.BeginGroup = False
            buttonVL03N.Tag = "Run VL03N"
            buttonVL03N.Enabled = True
        End If

        If buttonIW53 IsNot Nothing Then
            With buttonIW53
                .Caption = "IW53"
                .BeginGroup = False
                .Tag = "Run IW53"
                .Enabled = True
            End With
        End If
    End Sub

我尝试了以下

buttonVL03N = TryCast(myMenu.Controls.Add(MsoControlType.msoControlButton, Id:=1, Before:=3, Temporary:=True), CommandBarButton)

但是显然这不是那么简单

1 个答案:

答案 0 :(得分:0)

    buttonVL03N = TryCast(myMenu.CommandBar.Controls.Add(MsoControlType.msoControlButton, Id:=1, Temporary:=True), CommandBarButton)
    buttonIW53 = TryCast(myMenu.CommandBar.Controls.Add(MsoControlType.msoControlButton, Id:=1, Temporary:=True), CommandBarButton)

工作正常