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

广广

我试图根据这篇文章在Microsoft 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();
    }
}

}

当我右键单击一个字母时的结果:

在此处输入图片说明

但是用桌子我做不到。查看屏幕截图,了解我的意思:

在此处输入图片说明

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

对不起我的英语,...

埃西

好,终于我成功修复了它。

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

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

因此,您必须通过foreach更新代码,以在每种ContextMenu类型上迭代创建新的commandBarButtons,例如在代码下在启动函数上使用它

                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;
    }

}

希望对您有帮助。;->

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章

如何在系统菜单中添加菜单项

如何在Android中动态添加菜单项

如何在Office-2007的Office按钮中添加自定义菜单项?

如何添加菜单项

如何在Delphi的IDE上下文菜单中添加菜单项

如何使用Android导航组件添加菜单项

在ASP.NET Core 2.1中,如何在用户基于角色登录后添加菜单项?

如何在不同文件管理器的右键单击上添加菜单项

Angular 8 + PrimeNG PanelMenu-如何动态添加菜单项

如何在Microsoft Office Word 2010中启动OCR扫描?

如何在odoo中添加顶级菜单项

Microsoft Office / Word:如何在/ *和* /之间查找文本?

如何在Microsoft Office Online中打开附件?

如何在Microsoft Office Communicator中禁用表情符号

如何在Microsoft Office中搜索多行字符串?

如何在片段中添加菜单?

如何在活动中添加菜单

如何在上下文菜单中添加“打开链接”菜单项

如何在Visual Studio Code的“文件”菜单中添加新菜单项?

如何在“应用程序”菜单中添加系统范围的菜单项?

如何在wxPython中将子菜单项添加到弹出菜单项?

如何在应用程序工具栏中增加菜单项图标和标题之间的填充或边距?

如何在SSIS C#脚本任务中添加Microsoft Office Interop

在django会话中添加菜单项列表

如何在管理屏幕子菜单项中添加自定义HTML类名称?

Flutter如何在导航抽屉中添加可扩展菜单项?

如何在Android工具栏中添加单个菜单项?

如何在Android的导航抽屉中添加可折叠菜单项?

如何在Odoo 11的菜单项中添加多个组