获取Word中菜单项的宏命令名称

时间:2013-02-26 08:24:15

标签: c# vba ms-word vsto

我正在使用VSTO为MS Word 2010开发一个插件。 Word有一个“自定义键盘”对话框:

MS Word 2010 Customize Keyboard dialog

“命令”列表包含内置的宏命令,每个命令都分配给某个菜单或操作。它们可以使用Application.Run()方法从VSTO执行。

我需要获取当前安装的Word实例的“菜单项名称” - “宏命令名称” - “键盘快捷键”形式的记录。

到目前为止我尝试了什么:

Application.CustomizationContext = Application.NormalTemplate;
foreach (CommandBar bar in Application.CommandBars)
{
    // Name of menu group
    Application.Selection.InsertAfter(bar.NameLocal + "\n");
    foreach (CommandBarControl control in bar.Controls)
    {
        // Human-readable name
        Application.Selection.InsertAfter("\nName:" + control.accName
                   // Broad description
                   + "\nDescription:" + control.DescriptionText
                   // Keyboard shortcut
                   + "\nShortcut:" + control.accKeyboardShortcut);
    }
}

不幸的是CommandBarControl不包含宏命令名字段。我想知道如何收集这些并粘合在一起?

1 个答案:

答案 0 :(得分:1)

好的,现在是时候回答我的另一个问题,这个问题引起了太多关注。

每个版本的Microsoft Office都有一堆Excel表,其中包含ID,命令名称,默认快捷方式和一些其他信息。这是2010 version。使用提供的表格,现在可以轻松获得我们所需的一切:

var id = // Obtain from downloaded bundle
var name = // Obtain from downloaded bundle
var control = Application.CommandBars.FindControl(Id:id);
var description = control.DescriptionText;
var caption = control.Caption;
var shortcut = control.accKeyboardShortcut;
var parentControl = control.Parent;