How to: Add Commands to Office Shortcut Menus
Applies to |
---|
The information in this topic applies only to the specified Visual Studio Tools for Office projects and versions of Microsoft Office. Project type
Microsoft Office version
For more information, see Features Available by Application and Project Type. |
This example demonstrates how to add a command to a shortcut menu in Word by using an application-level add-in. The shortcut menu appears when you right-click a document.
Add the following code to the ThisAddIn class in an application-level add-in project for Word.
Example
Private MyApplication As Word.Application
Private WithEvents myControl As Office.CommandBarButton
Private customTemplate As Word.Template
Private Sub ThisAddIn_Startup _
(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Startup
MyApplication = Me.Application
GetCustomTemplate()
RemoveExistingMenuItem()
AddMenuItem()
End Sub
Private Sub GetCustomTemplate()
Dim TemplatePath As String = Environment.GetFolderPath _
(Environment.SpecialFolder.MyDocuments) + "\MyCustomTemplate.dot"
Try
MyApplication.AddIns(TemplatePath).Installed = True
Catch ex As Exception
MyApplication.AddIns.Add(TemplatePath, True)
End Try
customTemplate = MyApplication.Templates.Item(TemplatePath)
End Sub
Private Sub RemoveExistingMenuItem()
Dim contextMenu As Office.CommandBar = _
MyApplication.CommandBars("Text")
MyApplication.CustomizationContext = customTemplate
Dim control As Office.CommandBarButton = contextMenu.FindControl _
(Office.MsoControlType.msoControlButton, System.Type.Missing, _
"MyMenuItem", True, True)
If Not (control Is Nothing) Then
control.Delete(True)
End If
End Sub
Private Sub AddMenuItem()
MyApplication.CustomizationContext = customTemplate
Dim menuItem As Office.MsoControlType = _
Office.MsoControlType.msoControlButton
myControl = CType(MyApplication.CommandBars("Text").Controls.Add _
(menuItem, 1, True), Office.CommandBarButton)
myControl.Style = Office.MsoButtonStyle.msoButtonCaption
myControl.Caption = "My Menu Item"
myControl.Tag = "MyMenuItem"
customTemplate.Saved = True
End Sub
Sub myControl_Click(ByVal Ctrl As Microsoft.Office.Core.CommandBarButton, _
ByRef CancelDefault As Boolean) Handles myControl.Click
System.Windows.Forms.MessageBox.Show("My Menu Item clicked")
End Sub
private Word.Application myApplication;
private Office.CommandBarButton myControl;
private Word.Template customTemplate;
private void ThisAddIn_Startup(object sender, System.EventArgs e)
{
myApplication = this.Application;
GetCustomTemplate();
RemoveExistingMenuItem();
AddMenuItem();
}
private void GetCustomTemplate()
{
object TemplatePath = Environment.GetFolderPath
(Environment.SpecialFolder.MyDocuments) +
"\\MyCustomTemplate.dot";
object install = true;
if (myApplication.AddIns.get_Item(ref TemplatePath).Installed != true)
{
myApplication.AddIns.Add(TemplatePath.ToString(), ref install);
}
customTemplate = myApplication.Templates.get_Item(ref TemplatePath);
}
private void RemoveExistingMenuItem()
{
Office.CommandBar contextMenu = myApplication.CommandBars["Text"];
myApplication.CustomizationContext = customTemplate;
Office.CommandBarButton control =
(Office.CommandBarButton)contextMenu.FindControl
(Office.MsoControlType.msoControlButton, missing,
"MyMenuItem", true, true);
if ((control != null))
{
control.Delete(true);
}
}
private void AddMenuItem()
{
myApplication.CustomizationContext = customTemplate;
Office.MsoControlType menuItem =
Office.MsoControlType.msoControlButton;
myControl =
(Office.CommandBarButton)myApplication.CommandBars["Text"].Controls.Add
(menuItem,missing, missing, 1, true);
myControl.Style = Office.MsoButtonStyle.msoButtonCaption;
myControl.Caption = "My Menu Item";
myControl.Tag = "MyMenuItem";
myControl.Click +=
new Microsoft.Office.Core._CommandBarButtonEvents_ClickEventHandler
(myControl_Click);
customTemplate.Saved = true;
}
void myControl_Click(Microsoft.Office.Core.CommandBarButton Ctrl,
ref bool CancelDefault)
{
System.Windows.Forms.MessageBox.Show("My Menu Item clicked");
}
Robust Programming
You must set the Tag property of your controls when you add event handlers. Office uses the Tag property to keep track of event handlers for a specific CommandBarControl. If the Tag property is blank, the events are not handled properly.
Declare your menu variables at the class level instead of inside the method where they are called. This ensures that the menu variables will remain in scope as long as the application is running. Otherwise, the item is removed by garbage collection, and your event handler code stops working.
For Word solutions, set the CustomizationContext property of the Application object to the same document or template every time that you add or remove a command.
See Also
Tasks
How to: Create Office Toolbars Programmatically
Walkthrough: Creating Shortcut Menus for Bookmarks
How to: Add Custom Menus and Menu Items to Outlook
How to: Add Custom Icons to Toolbar and Menu Items
Concepts
The Variable missing and Optional Parameters in Office Solutions