Introduction
This is a very simple article, which is about how you can add a menu item in Microsoft Office Word 7/10 application context menu. Here we will discuss some custom event & delegates used for adding menu item.
The concept is simple; we will create an addIn project using Microsoft Visual Studio 2010. If you are not familiar with creating an addIn project, I would like to request you to read the article from the link below:
Background
Anyway, what are we actually doing in this article? Let’s make it clear first, we are trying to implement the functionalities listed below:
- We will create an AddIn for Microsoft Office Word.
- We will write few function/methods for adding a menu item in Microsoft Office Word application context menu.
- We will write a custom event for that item.
- We will write some text into the current open document.
That’s it, so let’s start to write code.
Using the Code
For this purpose, we will use a delegate ApplicationEvents4_WindowBeforeRightClickEventHandler
And a button event handler _CommandBarButtonEvents_ClickEventHandler
.
Actually, we created two custom events:
MyButton_Click()
App_WindowBeforeRightClick()
The first event, i.e.,"MyButton_Click()
" will just display a message box with a simple text message to ensure that you click on your custom menu item. The event "App_WindowBeforeRightClick()
" will be fired when you right click on your Microsoft Word document. In code, we write a method "AddItem()
" which is actually add the menu item into the Microsoft Office Word application context menu.
More information about ApplicationEvents4_WindowBeforeRightClickEventHandler
could be found at this link.
More information about _CommandBarButtonEvents_ClickEventHandler
could be found at this link.
The following figure A shows the output of the add-in program.
Figure A
A sample code snippet is given below:
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
private void InternalStartup()
{
this.Startup += new System.EventHandler(ThisAddIn_Startup);
this.Shutdown += new System.EventHandler(ThisAddIn_Shutdown);
}
#endregion
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();
}
}
History
- 29th Aug 2011: Initial post