Export (0) Print
Expand All

How to: Create Office Toolbars

This example creates a toolbar in Microsoft Office Outlook. The toolbar, which contains two buttons, appears at the top of the application. When you click a button, the code displays a message that shows the button caption.

Applies to: The information in this topic applies to application-level projects for the following applications: InfoPath 2007, Outlook 2007, Project 2007, and Visio 2007. For more information, see Features Available by Office Application and Project Type.

Although this example is specific to Outlook, the portion of this code that creates toolbars can be used to create toolbars for any of the applications listed above.


Office.CommandBar newToolBar;
Office.CommandBarButton firstButton;
Office.CommandBarButton secondButton;
Outlook.Explorers selectExplorers;

private void ThisAddIn_Startup(object sender, System.EventArgs e)
{

    selectExplorers = this.Application.Explorers;
    selectExplorers.NewExplorer += new Outlook
        .ExplorersEvents_NewExplorerEventHandler(newExplorer_Event);
    AddToolbar();
}

private void newExplorer_Event(Outlook.Explorer new_Explorer)
{
    ((Outlook._Explorer)new_Explorer).Activate();
    newToolBar = null;
    AddToolbar();
}

private void AddToolbar()
{

    if (newToolBar == null)
    {
        Office.CommandBars cmdBars =
            this.Application.ActiveExplorer().CommandBars;
        newToolBar = cmdBars.Add("NewToolBar",
            Office.MsoBarPosition.msoBarTop, false, true);
    }
    try
    {
        Office.CommandBarButton button_1 =
            (Office.CommandBarButton)newToolBar.Controls
            .Add(1, missing, missing, missing, missing);
        button_1.Style = Office
            .MsoButtonStyle.msoButtonCaption;
        button_1.Caption = "Button 1";
        button_1.Tag = "Button1";
        if (this.firstButton == null)
        {
            this.firstButton = button_1;
            firstButton.Click += new Office.
                _CommandBarButtonEvents_ClickEventHandler
                (ButtonClick);
        }

        Office.CommandBarButton button_2 = (Office
            .CommandBarButton)newToolBar.Controls.Add
            (1, missing, missing, missing, missing);
        button_2.Style = Office
            .MsoButtonStyle.msoButtonCaption;
        button_2.Caption = "Button 2";
        button_2.Tag = "Button2";
        newToolBar.Visible = true;
        if (this.secondButton == null)
        {
            this.secondButton = button_2;
            secondButton.Click += new Office.
                _CommandBarButtonEvents_ClickEventHandler
                (ButtonClick);
        }
    }
    catch (Exception ex)
    {
        MessageBox.Show(ex.Message);
    }
}

private void ButtonClick(Office.CommandBarButton ctrl,
        ref bool cancel)
{
    MessageBox.Show("You clicked: " + ctrl.Caption);
}


Declare your command bar variables at the class level instead of inside the method where they are called. This ensures that the command bar 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 does not run.

Community Additions

ADD
Show:
© 2014 Microsoft