次の方法で共有


方法: Word のショートカット メニューにコマンドを追加する

この例では、アプリケーション レベルのアドインを使用して、Word のショートカット メニューにコマンドを追加する方法を示します。 このショートカット メニューは、文書を右クリックすると表示されます。

対象: このトピックの情報は、Word 2007 と Word 2010 のドキュメント レベルのプロジェクトおよびアプリケーション レベルのプロジェクトに適用されます。詳細については、「Office アプリケーションおよびプロジェクト タイプ別の使用可能な機能」を参照してください。

Word 用のアプリケーション レベルのアドイン プロジェクトの ThisAddIn クラスに、次のコードを追加します。 このコードを実行するには、My Documents フォルダー (Windows XP 以前のバージョンの場合) または Documents フォルダー (Windows XP よりも新しいバージョンの場合) に 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 では、特定の CommandBarControl のイベント ハンドラーを追跡するために Tag プロパティが使用されます。 Tag プロパティが空の場合、イベントは正しく処理されません。

メニュー変数は、それらが呼び出されるメソッドの内部ではなく、クラス レベルで宣言します。 そうすることにより、アプリケーションが実行している間、メニュー変数がスコープ内に維持されます。 このようにしないと、項目はガベージ コレクションによって削除され、イベント ハンドラーのコードは実行を停止します。

コマンドを追加または削除するたびに、Application オブジェクトの CustomizationContext プロパティを同じドキュメントまたはテンプレートに設定します。

参照

処理手順

方法: Office ツール バーを作成する

チュートリアル : ブックマークのショートカット メニューの作成

方法: Excel のショートカット メニューにコマンドを追加する

方法 : Outlook にカスタム メニューとメニュー項目を追加する

方法 : カスタム アイコンをツール バーおよびメニュー項目に追加する

概念

Office UI のカスタマイズ

Office ソリューションの省略可能なパラメーター