65.9K
CodeProject 正在变化。 阅读更多。
Home

如何在 Microsoft Office Word 7/10 应用程序上下文菜单中添加菜单项

starIconstarIconstarIconstarIcon
emptyStarIcon
starIcon

4.79/5 (10投票s)

2011 年 8 月 28 日

CPOL

2分钟阅读

viewsIcon

65651

downloadIcon

1602

如何在 Microsoft Office Word 7/10 应用程序上下文菜单中添加菜单项

引言

这是一篇非常简单的文章,讲述了如何在 Microsoft Office Word 7/10 应用程序上下文菜单中添加菜单项。 在这里,我们将讨论一些用于添加菜单项的自定义事件和委托。

概念很简单;我们将使用 Microsoft Visual Studio 2010 创建一个插件项目。如果您不熟悉创建插件项目,我想请您阅读以下链接中的文章

背景

无论如何,我们实际上在本文中做什么? 让我们先明确一点,我们正在尝试实现以下列出的功能

  1. 我们将为 Microsoft Office Word 创建一个插件。
  2. 我们将编写一些函数/方法,用于在 Microsoft Office Word 应用程序上下文菜单中添加菜单项。
  3. 我们将为该项目编写一个自定义事件。
  4. 我们将一些文本写入当前打开的文档。

就这样,让我们开始编写代码吧。

Using the Code

为此,我们将使用委托 ApplicationEvents4_WindowBeforeRightClickEventHandler
和一个按钮事件处理程序 _CommandBarButtonEvents_ClickEventHandler

实际上,我们创建了两个自定义事件

  1. MyButton_Click()
  2. App_WindowBeforeRightClick()

第一个事件,即“MyButton_Click()”将仅显示一个带有简单文本消息的消息框,以确保您单击了自定义菜单项。 当您右键单击 Microsoft Word 文档时,将触发事件“App_WindowBeforeRightClick()”。 在代码中,我们编写了一个方法“AddItem()”,该方法实际上是将菜单项添加到 Microsoft Office Word 应用程序上下文菜单中。

有关 ApplicationEvents4_WindowBeforeRightClickEventHandler 的更多信息可以在 此链接中找到。

有关 _CommandBarButtonEvents_ClickEventHandler 的更多信息可以在 此链接中找到。

下图 A 显示了插件程序的输出。

图A

ctx-menu.png

以下是一个示例代码片段

public partial class ThisAddIn
    {
        _CommandBarButtonEvents_ClickEventHandler eventHandler;

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

        private 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 = "Hello !!!";
                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", "Hello !!!");
            Globals.ThisAddIn.Application.Selection.InsertAfter
			("I love CodeProject" + Environment.NewLine);
            Globals.ThisAddIn.Application.Selection.InsertAfter
			("Author: " + "Md. Marufuzzaman" + Environment.NewLine);
            Globals.ThisAddIn.Application.Selection.InsertAfter
			("Thanks To : " +  Environment.UserName + Environment.NewLine);
            Globals.ThisAddIn.Application.Selection.InsertAfter
			("Current time is :" +  DateTime.Now.ToLongTimeString() +
			Environment.NewLine);
            RemoveItem();
        }
    }

历史

  • 2011 年 8 月 29 日:初始发布
© . All rights reserved.