如何在Microsoft Office Word中添加菜单项

问题描述:

我试图根据this的帖子在Microsoft Word中创建一个右键单击菜单项。如何在Microsoft Office Word中添加菜单项

这里是我的代码:

private void ThisAddIn_Startup(object sender, System.EventArgs e) 
    { 
     try 
     { 
      eventHandler = new _CommandBarButtonEvents_ClickEventHandler(MyButton_Click); 
      Word.Application applicationObject = Globals.ThisAddIn.Application as Word.Application; 
      applicationObject.WindowBeforeRightClick += new Microsoft.Office.Interop.Word.ApplicationEvents4_WindowBeforeRightClickEventHandler(App_WindowBeforeRightClick); 
     } 
     catch (Exception exception) 
     { 
      MessageBox.Show("Error: " + exception.Message); 
     } 
    } 

    void App_WindowBeforeRightClick(Microsoft.Office.Interop.Word.Selection Sel, ref bool Cancel) 
    { 
     try 
     { 
      this.AddItem(); 
     } 
     catch (Exception exception) 
     { 
      MessageBox.Show("Error: " + exception.Message); 
     } 

    } 
    private void AddItem() 
    { 
     Word.Application applicationObject = Globals.ThisAddIn.Application as Word.Application; 
     CommandBarButton commandBarButton = applicationObject.CommandBars.FindControl(MsoControlType.msoControlButton, missing, "HELLO_TAG", missing) as CommandBarButton; 
     if (commandBarButton != null) 
     { 
      System.Diagnostics.Debug.WriteLine("Found button, attaching handler"); 
      commandBarButton.Click += eventHandler; 
      return; 
     } 
     CommandBar popupCommandBar = applicationObject.CommandBars["Text"]; 
     bool isFound = false; 
     foreach (object _object in popupCommandBar.Controls) 
     { 
      CommandBarButton _commandBarButton = _object as CommandBarButton; 
      if (_commandBarButton == null) continue; 
      if (_commandBarButton.Tag.Equals("HELLO_TAG")) 
      { 
       isFound = true; 
       System.Diagnostics.Debug.WriteLine("Found existing button. Will attach a handler."); 
       commandBarButton.Click += eventHandler; 
       break; 
      } 
     } 
     if (!isFound) 
     { 
      commandBarButton = (CommandBarButton)popupCommandBar.Controls.Add(MsoControlType.msoControlButton, missing, missing, missing, true); 
      System.Diagnostics.Debug.WriteLine("Created new button, adding handler"); 
      commandBarButton.Click += eventHandler; 
      commandBarButton.Caption = "h5"; 
      commandBarButton.FaceId = 356; 
      commandBarButton.Tag = "HELLO_TAG"; 
      commandBarButton.BeginGroup = true; 
     } 
    } 

    private void RemoveItem() 
    { 
     Word.Application applicationObject = Globals.ThisAddIn.Application as Word.Application; 
     CommandBar popupCommandBar = applicationObject.CommandBars["Text"]; 
     foreach (object _object in popupCommandBar.Controls) 
     { 
      CommandBarButton commandBarButton = _object as CommandBarButton; 
      if (commandBarButton == null) continue; 
      if (commandBarButton.Tag.Equals("HELLO_TAG")) 
      { 
       popupCommandBar.Reset(); 
      } 
     } 
    } 
    private void ThisAddIn_Shutdown(object sender, System.EventArgs e) 
    { 
     Word.Application App = Globals.ThisAddIn.Application as Word.Application; 
     App.WindowBeforeRightClick -= new Microsoft.Office.Interop.Word.ApplicationEvents4_WindowBeforeRightClickEventHandler(App_WindowBeforeRightClick); 

    } 

    #region VSTO generated code 

    /// <summary> 
    /// Required method for Designer support - do not modify 
    /// the contents of this method with the code editor. 
    /// </summary> 
    private void InternalStartup() 
    { 
     this.Startup += new System.EventHandler(ThisAddIn_Startup); 
     this.Shutdown += new System.EventHandler(ThisAddIn_Shutdown); 
    } 
    #endregion 
    //Event Handler for the button click 

    private void MyButton_Click(CommandBarButton cmdBarbutton, ref bool cancel) 
    { 
     System.Windows.Forms.MessageBox.Show("Hello !!! Happy Programming", "l19 !!!"); 
     RemoveItem(); 
    } 
} 

}

,当我右键点击一个字母结果:

enter image description here

但有一张桌子,我不能做到这一点。退房的屏幕截图,看看我的意思是:

enter image description here

我无法添加项目菜单,当我用鼠标右键单击MS Word中的表格。请帮帮我。 谢谢!

对不起我的英语,...

+0

https://msdn.microsoft.com/zh-cn/office/aa905340.aspx – MickyD

+0

你是什么意思,“用表我不能做”?实际发生了什么,你期望什么?你屏幕截图中的箭头是什么意思? –

+0

我想在右键菜单中添加一个菜单项。用桌子我不能这样做。我想知道在ms word 2013中做的代码。 – Quang

Word维护多个上下文菜单。您可以通过在Application.CommandBars其位置枚举所有CommandBar对象看到它们是msoBarPopup

foreach (var commandBar in applicationObject.CommandBars.OfType<CommandBar>() 
           .Where(cb => cb.Position == MsoBarPosition.msoBarPopup)) 
{ 
    Debug.WriteLine(commandBar.Name); 
} 

被链接的示例中使用的命令栏是一个名为“文本”,这一条涉及上下文当您右键单击段落文本中的某个位置时弹出菜单。

然而,添加的东西你有你的按钮添加到相应的表相关的上下文菜单表的上下文菜单。表具有取决于选择什么不同的上下文菜单当你点击:

  • applicationObject.CommandBars [ “表”]
  • applicationObject.CommandBars [ “表中的文本”]
  • applicationObject.CommandBars [“表细胞 “]
  • applicationObject.CommandBars [” 表格标题 “]
  • applicationObject.CommandBars [” 表列出 “]
  • applicationObject.CommandBars [” 表图片“]

所以我建议你提取一个方法,将一个按钮添加到CommandBar然后你调用所有的命令栏,你想添加按钮的方法。像下面这样:

private void AddButton(CommandBar popupCommandBar) 
{ 
    bool isFound = false; 
    foreach (var commandBarButton in popupCommandBar.Controls.OfType<CommandBarButton>()) 
    { 
     if (commandBarButton.Tag.Equals("HELLO_TAG")) 
     { 
      isFound = true; 
      Debug.WriteLine("Found existing button. Will attach a handler."); 
      commandBarButton.Click += eventHandler; 
      break; 
     } 
    } 
    if (!isFound) 
    { 
     var commandBarButton = (CommandBarButton)popupCommandBar.Controls.Add 
      (MsoControlType.msoControlButton, missing, missing, missing, true); 
     Debug.WriteLine("Created new button, adding handler"); 
     commandBarButton.Click += eventHandler; 
     commandBarButton.Caption = "Hello !!!"; 
     commandBarButton.FaceId = 356; 
     commandBarButton.Tag = "HELLO_TAG"; 
     commandBarButton.BeginGroup = true; 
    } 
} 

// add the button to the context menus that you need to support 
AddButton(applicationObject.CommandBars["Text"]); 
AddButton(applicationObject.CommandBars["Table Text"]); 
AddButton(applicationObject.CommandBars["Table Cells"]); 

正如德克表示,你需要点击编辑链接你原来的问题下,贴在你的“答案”的信息在它的结束,然后删除了“答案” - 这是不是答案...

我的答案基于您提供的附加信息。这显然是一个VSTO应用程序级插件。因此,对于Office 2013,您需要使用功能区XML创建自定义菜单。这不能使用功能区设计器来完成,所以如果您已经有功能区设计器,则需要将其转换为功能区XML。你会发现如何将VSTO文档中这样做的一篇文章: https://msdn.microsoft.com/en-us/library/aa942866.aspx

如何使用功能区XML定制上下文菜单的信息可以在这个MSDN文章中找到: https://msdn.microsoft.com/en-us/library/ee691832(v=office.14)

总结:您需要为Ribbon XML添加<contextMenus>元素,并为每个要添加或更改的菜单项添加<contextMenu>元素。元素的idMso属性指定WHICH上下文菜单。您可以在Microsoft网站的下载中找到ControlIds列表(idMso的值): https://www.microsoft.com/en-us/download/details.aspx?id=36798

FWIW该上下文菜单的ControlId可能是ContextMenuTextTable。

+0

对不起, 我还没有做,帮帮我。使用功能区我仍然无法添加项目菜单,当我右键单击ms字表时 – Quang

+0

单击原始问题下的“编辑”链接,并在结尾处粘贴添加到的功能区XML你的项目。另外,请确定表格单元格中您尝试右键单击的内容的类型。单词可以区分很多事情 - 例如是否标记了拼写错误 - 并且为所有可能性都有单独的右键单击菜单。您可能需要将控件添加到多个上下文菜单中,以便在所有情况下都能显示... –