如何在 Microsoft Office Word 中添加菜单项 [英] How to Add a Menu Item in Microsoft Office Word

查看:80
本文介绍了如何在 Microsoft Office Word 中添加菜单项的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我尝试基于

但是有一张桌子我做不到.查看屏幕截图以了解我的意思:

当我在 ms word 表格上单击鼠标右键时,我无法添加项目菜单.请帮我.谢谢!!

对不起我的英语,...

解决方案

好的,我终于成功解决了.

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

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

的索引

因此,您必须通过 foreach 更新您的代码以迭代创建新的 commandBarButtons在每种类型的 ContextMenu 上像下面的代码一样在你的启动功能上使用这个

 试试{列表mindex = new List() { 105, 120, 127, 117, 108, 99, 134 };foreach (var item in mindex){AddItemGeneral(applicationObject.CommandBars[item], youreventHandler, "yourTagLabelplusaDiffNumber" + item.ToString(), "your Caption");}}捕获(异常异常){MessageBox.Show("错误:" + 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;}别的{commandBarButton.Click += MyEvent;}}

希望对大家有帮助.;->

I tried to create a right-click menu item in Microsoft Word based on this post.

Here is my code:

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

}

and the result when i right click on a letter :

But with a table I cannot do it. Check out the screenshot to see what I mean:

I can not add the item menu when i right click on a table of ms word. Please help me. Thank!!

sorry about my english,...

解决方案

OK, Finally I have successed to fix it.

first of all, there are more than 200 different contextmenu for RightClick

but the common type of word.application.CommandBars are base on under Indexes { 105, 120, 127, 117, 108, 99, 134 }; these are contains the indexes of text, table, heading, textBox and ...

so, you must update your code by foreach to iterate the create new commandBarButtons on each type of ContextMenu someting like under code use this on your Startup Function

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

Finally,

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

}

I hope it helps you guys. ;->

这篇关于如何在 Microsoft Office Word 中添加菜单项的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆