将子菜单添加到自定义右键菜单

将子菜单添加到自定义右键菜单

本文介绍了Excel VBA-将子菜单添加到自定义右键菜单的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

长时间观看者,首次发布者.我有一个带有右键单击功能的表单,可以正常工作.我试图将一个子菜单添加到主右键单击菜单中,以分隔一些功能/命令.我需要/想要插入选择案例"的部分,但是,它仅显示顶部菜单.不知道从这里去哪里.任何帮助都很棒

Long time viewer, first time poster.I have a form with Right-Click functions that work fine.I'm trying to add a sub menu to the main right-click menu to separate some functions/command. I need/want to insert the section where the 'Select Case' is, however, its only showing the top menu.Not sure where to go from here. Any help would be awesome

谢谢:)

P.S.如果需要,我很乐意进一步解释.

P.S. I would be happy to explain further if needed.

Sub fzCopyPaste(iItems As Integer)
On Error Resume Next
CommandBars("Custom").Delete
Set PopBar = CommandBars.Add(Name:="Custom", Position:=msoBarPopup, MenuBar:=False, Temporary:=True)

'在主Popbar上添加top_menu:可以正常工作

'Add top_menu on Main Popbar : This work fine

Set top_menu = PopBar.Controls.Add(Type:=msoControlButton)
With top_menu
    '.FaceId =
    .Caption = "&Some Commands"
End With

需要将以下子菜单插入顶部菜单但是什么都没显示:不起作用

Need to Insert the below sub menu(s) into the top menuBut nothing shows up : Does not work

Select Case iItems
Case 1  ' Copy and Paste
    Set copy_button = top_menu.Controls.Add(Type:=msoControlButton)
    With copy_button
        .FaceId = 19
        .Caption = "&Copy"
        .Tag = "tCopy"
        .OnAction = "fzCopyOne(true)"
    End With

    Set paste_button = top_menu.Controls.Add(Type:=msoControlButton)
    With paste_button
        .FaceId = 22
        .Tag = "tPaste"
        .Caption = "&Paste"
        .OnAction = "fzCopyOne(true)"
    End With
Case 2 '  Paste Only
    Set paste_button = top_menu.Controls.Add(Type:=msoControlButton)
    With paste_button
        .FaceId = 22
        .Tag = "tPaste"
        .Caption = "&Paste"
        .OnAction = "fzCopyOne(true)"
    End With
End Select

'这里的其他顶级菜单:正常工作

'Extra top menue(s) below here : This work fine

    Set paste_button = PopBar.Controls.Add(Type:=msoControlButton)
    With paste_button
        .FaceId = 22
        .Tag = "tPaste"
        .Caption = "Main POP BAR 2"
        .OnAction = "fzCopyOne(true)"
    End With


PopBar.ShowPopup

CommandBars("Custom").Delete
End Sub

推荐答案

您将Copy_Button设置为等于msoControlButton.如果您想要一个按钮,这是正确的.但是,您需要菜单,因此应将其设置为msoControlPopup.尝试这样的事情:

You set Copy_Button equal to an msoControlButton. If you want a button, this is correct. You want a menu though, so you should set it to an msoControlPopup. Try something like this:

Set Top_Menu = PopBar.Controls.Add(Type:=msoControlPopup)
With Top_Menu
    .Caption = "&Some Commands"
    Set MySubMenu = .Controls.Add(Type:=msoControlPopup, before:=1, temporary:=True)
    Select Case iItems
        Case 1
            With MySubMenu
                .Caption = "Submenu Commands"
                With .Controls.Add(Type:=msoControlButton, before:=1, temporary:=True)
                    .FaceId = 19
                    .Caption = "&Copy"
                    .Tag = "tCopy"
                    .OnAction = "fzCopyOne(true)"
                End With

                With .Controls.Add(Type:=msoControlButton, before:=2, temporary:=True)
                    .FaceId = 22
                    .Tag = "tPaste"
                    .Caption = "&Paste"
                    .OnAction = "fzCopyOne(true)"
                End With
            End With
        Case 2
            'etc
    End Select
End With

我用以下内容删除了"Top_Menu"部分(前三行);它添加了一个额外的按钮,然后添加了所需的菜单.

I removed the "Top_Menu" section (first 3 lines) with the below; it was adding an extra button and then the desired menu.

Set MySubMenu = PopBar.Controls.Add(Type:=msoControlPopup, before:=1, temporary:=True)
With MySubMenu
    .Caption = "&Some Commands"

这篇关于Excel VBA-将子菜单添加到自定义右键菜单的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-14 13:58