Excel + VBA:单击自动图形上方时更改右键单击菜单

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/649681/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-11 10:17:04  来源:igfitidea点击:

Excel + VBA: changing right click menu when clicking above autoshapes

excelvbaexcel-vba

提问by praavDa

I am doing a little Excel + vba application and I have got stuck. I know that using

我正在做一个 Excel + vba 应用程序,但我被卡住了。我知道使用

Application.CommandBars("Cell").Controls.* 

I can change right click menu to only show specific options for specific cell (of course with additional code).

我可以更改右键菜单以仅显示特定单元格的特定选项(当然还有附加代码)。

But is there a way to change the menu when I click right mouse button above autoshape?

但是,当我在 autoshape 上方单击鼠标右键时,有没有办法更改菜单?

I have been using something like

我一直在使用类似的东西

(...)
'checking autoshape position
sh_le = sh.Left
sh_to = sh.Top
sh_ri = sh.Left + sh.Width
sh_do = sh.Top + sh.Height

'checking clicked cell position
cc_le = cel.Left
cc_to = cel.Top
cc_ri = cel.Left + cel.Width
cc_do = cel.Top + cel.Height

If (sh_le <= cc_le) And (sh_to <= cc_to) And (sh_ri >= cc_ri) And (sh_do >= cc_do) Then  
 'build custom menu
end if

It looks good (at least I think so :) - but when I am clicking above shape, the Worksheet_BeforeRightClickis not starting. Is there any other way to do so? I would be grateful for any information.

它看起来不错(至少我是这么认为的 :) - 但是当我点击形状上方时,Worksheet_BeforeRightClick它没有开始。有没有其他方法可以做到这一点?我将不胜感激任何信息。

采纳答案by THEn

I have implemented this way. As in code OnAction = "openOrder"where openOrderis Public Module to do the job.

我已经实现了这种方式。在代码OnAction = "openOrder" 中openOrder是完成这项工作的公共模块。

You can change the CommandBars("Cell").Controlsto CommandBars("Shapes").Controls

您可以将CommandBars("Cell").Controls更改为CommandBars("Shapes").Controls

Private Sub Workbook_Deactivate()
    On Error Resume Next
    Application.CommandBars("Cell").Controls("View order").Delete
    On Error GoTo 0
End Sub

        Private Sub Workbook_SheetBeforeRightClick(ByVal Sh As Object, ByVal Target As Range, Cancel As Boolean)

        Dim cBut As CommandBarButton

        On Error Resume Next

        If IneedCustomMenu=TRUE Then
            On Error Resume Next
            Set cBut = Application.CommandBars("Cell").Controls.Add(Temporary:=True)
                With cBut
                   .Caption = "View order"
                   .Style = msoButtonCaption
                   .OnAction = "openOrder"
                End With
            On Error GoTo 0
        Else
               On Error Resume Next
               With Application
                        .CommandBars(Cell).Controls("View order").Delete
               End With
        End If
       End Sub

回答by Lunatik

There is an undocumented featurette that means this event may not fire until you have closed the file and reopened it.

有一个未记录的功能,这意味着在您关闭文件并重新打开它之前,此事件可能不会触发。

Give that a try.

试一试吧。