次の方法で共有


方法 : Office ツール バーをプログラムで作成する

更新 : 2007 年 11 月

対象

このトピックの情報は、指定された Visual Studio Tools for Office プロジェクトおよび Microsoft Office のバージョンにのみ適用されます。

プロジェクトの種類

  • ドキュメント レベルのプロジェクト

  • アプリケーション レベルのプロジェクト

Microsoft Office のバージョン

  • Microsoft Office 2003

詳細については、「アプリケーションおよびプロジェクトの種類別の使用可能な機能」を参照してください。

この例では、"Test" という名前のツール バーを Microsoft Office Word 2003 に作成します。このツール バーはドキュメントの中央付近に表示され、ボタンが 2 つあります。ボタンをクリックすると、メッセージ ボックスが表示されます。Microsoft Office Excel 2003 でユーザー インターフェイスをカスタマイズする方法の例については、「方法 : Office メニューをプログラムで作成する」を参照してください。

ThisDocument クラスに次のコードを追加します。

scff9c7c.alert_note(ja-jp,VS.90).gifメモ :

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

使用例

' Create the command bar variables at the class level.
Dim commandBar As Office.CommandBar
Dim firstButton As Office.CommandBarButton
Dim secondButton As Office.CommandBarButton


Private Sub ThisDocument_Startup(ByVal sender As Object, ByVal e As System.EventArgs) _
    Handles Me.Startup

    AddToolbar()
End Sub


Private Sub AddToolbar()

    Try
        commandBar = Me.CommandBars("Test")
    Catch ex As ArgumentException
        ' Toolbar named Test does not exist so we should create it.
    End Try

    If commandBar Is Nothing Then
        commandBar = Application.CommandBars.Add("Test", 1, False, True)
    End If

    Try
        ' Add a button to the command bar and create an event handler.
        firstButton = CType(commandBar.Controls.Add(1), Office.CommandBarButton)

        firstButton.Style = Office.MsoButtonStyle.msoButtonCaption
        firstButton.Caption = "button 1"
        firstButton.Tag = "button1"
        AddHandler firstButton.Click, AddressOf ButtonClick

        ' Add a second button to the command bar and create an event handler.
        secondButton = CType(commandBar.Controls.Add(1), Office.CommandBarButton)

        secondButton.Style = Office.MsoButtonStyle.msoButtonCaption
        secondButton.Caption = "button 2"
        secondButton.Tag = "button2"
        AddHandler secondButton.Click, AddressOf ButtonClick

        commandBar.Visible = True

    Catch ex As Exception
        MessageBox.Show(ex.Message)
    End Try
End Sub


' Handles the event when a button on the new toolbar is clicked.
Private Sub ButtonClick(ByVal ctrl As Office.CommandBarButton, ByRef Cancel As Boolean)

    MsgBox("You clicked: " & ctrl.Caption)
End Sub
// Create the command bar variables at the class level.
Office.CommandBar commandBar;
Office.CommandBarButton firstButton;
Office.CommandBarButton secondButton;

private void ThisDocument_Startup(object sender, System.EventArgs e)
{
    AddToolbar();
}

private void AddToolbar()
{
    try
    {
        commandBar = Application.CommandBars["Test"];
    }
    catch (ArgumentException e)
    {
        // Toolbar named Test does not exist so we should create it.
    }

    if (commandBar == null)
    {
        // Add a commandbar named Test.
        commandBar = Application.CommandBars.Add("Test", 1, missing, true);
    }

    try
    {
        // Add a button to the command bar and an event handler.
        firstButton = (Office.CommandBarButton)commandBar.Controls.Add(
            1, missing, missing, missing, missing);

        firstButton.Style = Office.MsoButtonStyle.msoButtonCaption;
        firstButton.Caption = "button 1";
        firstButton.Tag = "button1";
        firstButton.Click += new Office._CommandBarButtonEvents_ClickEventHandler(ButtonClick);

        // Add a second button to the command bar and an event handler.
        secondButton = (Office.CommandBarButton)commandBar.Controls.Add(
            1, missing, missing, missing, missing);

        secondButton.Style = Office.MsoButtonStyle.msoButtonCaption;
        secondButton.Caption = "button 2";
        secondButton.Tag = "button2";
        secondButton.Click += new Office._CommandBarButtonEvents_ClickEventHandler(ButtonClick);

        commandBar.Visible = true;
    }
    catch (ArgumentException e)
    {
        MessageBox.Show(e.Message);
    }
}

// Handles the event when a button on the new toolbar is clicked.
private void ButtonClick(Office.CommandBarButton ctrl, ref bool cancel)
{
    MessageBox.Show("You clicked: " + ctrl.Caption);
}

参照

処理手順

方法 : Office メニューをプログラムで作成する

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

概念

Office UI のカスタマイズ

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