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

时间:2015-12-15 02:51:19

标签: c# ms-word vsto

我尝试根据this帖子在Microsoft Word中创建一个右键单击菜单项。

这是我的代码:

extension BuySwagViewController: PKPaymentAuthorizationViewControllerDelegate {
  func paymentAuthorizationViewController(controller: PKPaymentAuthorizationViewController!, didAuthorizePayment payment: PKPayment!, completion: ((PKPaymentAuthorizationStatus) -> Void)!) {
    completion(PKPaymentAuthorizationStatus.Success)
  }

  func paymentAuthorizationViewControllerDidFinish(controller: PKPaymentAuthorizationViewController!) {
    controller.dismissViewControllerAnimated(true, completion: nil)
  }
}

}

当我右键点击一封信时的结果:

enter image description here

但是有了桌子,我无法做到。看看截图,看看我的意思:

enter image description here

当我右键单击ms字表时,我无法添加项目菜单。请帮我。 谢谢!

抱歉我的英语,...

3 个答案:

答案 0 :(得分:3)

Word维护多个上下文菜单。您可以通过枚举位置为CommandBar的{​​{1}}中的所有Application.CommandBars个对象来查看所有这些对象:

msoBarPopup

链接样本中使用的命令栏是名为" Text"这个与右键单击段落文本中的某个位置时弹出的上下文菜单有关。

但是,要在表的上下文菜单中添加内容,必须将按钮添加到相应的表相关上下文菜单中。表格具有不同的上下文菜单,具体取决于您单击时选择的内容:

  • applicationObject.CommandBars ["表"]
  • applicationObject.CommandBars ["表格文字"]
  • applicationObject.CommandBars [" Table Cells"]
  • applicationObject.CommandBars ["表格标题"]
  • applicationObject.CommandBars ["表列表"]
  • applicationObject.CommandBars [" Table Pictures"]

所以我建议您提取一个向foreach (var commandBar in applicationObject.CommandBars.OfType<CommandBar>() .Where(cb => cb.Position == MsoBarPosition.msoBarPopup)) { Debug.WriteLine(commandBar.Name); } 添加按钮的方法,然后使用要添加按钮的所有命令栏调用该方法。如下所示:

CommandBar

答案 1 :(得分:1)

正如Dirk所说,你需要点击原始问题下的编辑链接,将信息粘贴到你的答案中。在它的最后,然后删除&#34;答案&#34; - 这不是答案......

我的答案基于您提供的其他信息。这显然是VSTO应用程序级外接程序。因此,对于Office 2013,您需要使用Ribbon XML创建自定义菜单。使用功能区设计器无法完成此操作,因此如果您已有功能区设计器,则需要将其转换为功能区XML。您将在VSTO文档中找到有关如何执行此操作的文章: https://msdn.microsoft.com/en-us/library/aa942866.aspx

有关如何使用Ribbon XML自定义上下文菜单的信息,请参阅此MSDN文章: https://msdn.microsoft.com/en-us/library/ee691832(v=office.14)

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

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

答案 2 :(得分:1)

好的,最后我成功修复了它。

首先,RightClick有200多种不同的上下文菜单

,但是word.application.CommandBars的常见类型基于Indexes {105、120、127、117、108、99、134}; 这些包含文本,表,标题,textBox和...的索引。

因此,您必须通过foreach更新代码以迭代创建新的commandBarButtons 在每种类型的ContextMenu上 在代码下使用诸如此类的东西在您的启动功能上使用

                try
                {


                    List<int> mindex = new List<int>() { 105, 120, 127, 117, 108, 99, 134 };
                    foreach (var item in mindex)
                    {

                        AddItemGeneral(applicationObject.CommandBars[item], youreventHandler, "yourTagLabelplusaDiffNumber" + item.ToString(), "your Caption");                            
                    }



                }
                catch (Exception exception)
                {
                    MessageBox.Show("Error: " + exception.Message);
                }

最后,

private void AddItemGeneral(CommandBar popupCommandBar, _CommandBarButtonEvents_ClickEventHandler MyEvent, string MyTag,string MyCaption)
{

    CommandBarButton commandBarButton = popupCommandBar.CommandBars.FindControl(MsoControlType.msoControlButton, missing, MyTag, missing) as CommandBarButton;          
    if (commandBarButton == null)
    {

        commandBarButton = (CommandBarButton)popupCommandBar.Controls.Add(MsoControlType.msoControlButton, Missing.Value, Missing.Value, popupCommandBar.Controls.Count + 1, true);
        commandBarButton.Caption = MyCaption;
        commandBarButton.BeginGroup = true;
        commandBarButton.Tag = MyTag;
        commandBarButton.Click += MyEvent;
    }
    else
    {
        commandBarButton.Click += MyEvent;
    }

}

我希望它对你们有帮助。 ;->