vba 将项目添加到 Excel 图表中的右键单击菜单
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/1040630/
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
add item to right-click menu in Excel chart
提问by Jon Peltier
How to add item to right-click menu in Excel chart using VBA? Excel is 2007. Chart is standalone sheet.
如何使用VBA将项目添加到Excel图表中的右键单击菜单?Excel 是 2007。图表是独立的工作表。
回答by Jon Peltier
This is pretty late, but I thought it's important for anyone else trying to do this.
这已经很晚了,但我认为这对尝试这样做的其他人很重要。
In Excel 2007 and 2010, you cannot alter the context menus for charts and shapes. There are no errors, but the modifications just are not applied. Microsoft knows about this, at least the smart guys in the product group, but I believe there's nothing planned for these versions to address this issue.
在 Excel 2007 和 2010 中,您无法更改图表和形状的上下文菜单。没有错误,但只是没有应用修改。微软知道这一点,至少产品组中的聪明人知道,但我相信这些版本没有任何计划来解决这个问题。
回答by Alistair Knock
You want to use the CommandBars
collection, which is used for the context menus in addition to the toolbars. There are at least three context menus - cell (right-clicking on any cell), row and column (right clicking on the A, B, C or 1, 2, 3 headers). Very basic code for adding a new element to the row context menu is below.
您想要使用该CommandBars
集合,它除了工具栏之外还用于上下文菜单。至少有三个上下文菜单 - 单元格(右键单击任何单元格)、行和列(右键单击 A、B、C 或 1、2、3 标题)。将新元素添加到行上下文菜单的非常基本的代码如下。
See also the MSDN documentation for CommandBars, and there's an old Office 2000 list of commandbar elements.
另请参阅CommandBars的MSDN 文档,并且有一个旧的 Office 2000 命令栏元素列表。
Sub AddToContextMenu()
Dim element As CommandBarButton
Set element = CommandBars("row").Controls.Add
element.Caption = "Menu Item Name"
element.OnAction = "VBAToRunOnRow"
End Sub
回答by Charlie
I know you're not asking for a C# solution but I thought it'd be useful to post one since I just worked it out from Alistair's answer:
我知道您不是在要求 C# 解决方案,但我认为发布一个解决方案会很有用,因为我刚刚从 Alistair 的回答中解决了这个问题:
public void AddContextMenuItem(Microsoft.Office.Interop.Excel.Application application, string label)
{
var button = (CommandBarButton)application.CommandBars["cell"].Controls.Add();
button.Caption = label;
button.Click += button_Click;
}
void button_Click(CommandBarButton Ctrl, ref bool CancelDefault)
{
//Button click
}