如何:向 Word 中的快捷菜单添加命令
此示例演示如何使用应用程序级外接程序向 Word 中的快捷菜单添加命令。 此快捷菜单在您右击文档时出现。
**适用于:**本主题中的信息适用于 Word 2007 和 Word 2010 的文档级项目和应用程序级项目。有关更多信息,请参见按 Office 应用程序和项目类型提供的功能。
将以下代码添加到 Word 应用程序级外接程序项目的 ThisAddIn 类中。 若要运行此代码,名为 MyCustomTemplate.dotx 的 Word 模板必须位于“我的文档”文件夹(对于 Windows XP 和早期版本)或“文档”文件夹(对于较新版本的 Windows )中。
示例
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.dotx"
Dim install As Boolean = True
For Each installedTemplate As Word.Template In MyApplication.Templates
If installedTemplate.FullName = DirectCast(TemplatePath, String) Then
install = False
End If
Next
If install = True Then
MyApplication.AddIns.Add(TemplatePath.ToString(), True)
End If
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
GC.Collect()
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.dotx";
object install = true;
foreach (Word.Template installedTemplate in myApplication.Templates)
{
if (installedTemplate.FullName == (string)TemplatePath)
{
install = false;
}
}
if ((bool)install)
{
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;
GC.Collect();
}
void myControl_Click(Microsoft.Office.Core.CommandBarButton Ctrl,
ref bool CancelDefault)
{
System.Windows.Forms.MessageBox.Show("My Menu Item clicked");
}
可靠编程
添加事件处理程序时,必须在控件上设置 Tag 属性。 Office 使用 Tag 属性跟踪特定 CommandBarControl 的事件处理程序。 如果 Tag 属性为空,则没有正确地处理事件。
在类级别声明菜单变量,而不要在调用这些变量的方法内进行声明。 这样可以确保只要应用程序在运行,这些菜单变量就会保持在范围内。 否则,此项便会被垃圾回收移除,而事件处理程序代码也将停止工作。
每次添加或移除命令时,将 Application 对象的 CustomizationContext 属性设置为相同的文档或模板。