共用方式為


HOW TO:將命令加入到 Word 的捷徑功能表

在這個範例中,會示範如何使用應用程式層級增益集,將命令加入至 Word 的捷徑功能表。 當您以滑鼠右鍵按一下文件時,會出現捷徑功能表。

**適用於:**本主題中的資訊適用於 Word 2007 和 Word 2010 的文件層級專案和應用程式層級專案。如需詳細資訊,請參閱依 Office 應用程式和專案類型提供的功能

將下列程式碼加入至 Word 應用程式層級專案中的 ThisAddIn 類別。 [我的文件] 資料夾 (Windows XP 和舊版) 或 [文件] 資料夾 (新版 Windows) 中必須有名為 MyCustomTemplate.dotx 的 Word 範本,才能執行此程式碼。

範例

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 屬性設定為相同的文件或範本。

請參閱

工作

HOW TO:建立 Office 工具列

逐步解說:建立書籤的快速鍵功能表

HOW TO:將命令加入到 Excel 的捷徑功能表

HOW TO:將自訂功能表和功能表項目加入至 Outlook

HOW TO:將自訂圖示加入至工具列和功能表項目

概念

Office 方案中的選擇性參數

其他資源

Office UI 自訂