Comment : créer des barres d'outils Office par programme

Mise à jour : novembre 2007

S'applique à

Les informations de cette rubrique s'appliquent uniquement aux projets et versions Visual Studio Tools pour Office spécifiés de Microsoft Office.

Type de projet

  • Projets au niveau du document

  • Projets au niveau de l'application

Version de Microsoft Office

  • Microsoft Office 2003

Pour plus d'informations, consultez Fonctionnalités disponibles par type d'application et de projet.

Cet exemple crée une barre d'outils appelée Test dans Microsoft Office Word 2003. Elle apparaît à peu près au centre du document et contient deux boutons. Lorsqu'un utilisateur clique sur un bouton, un message apparaît. Pour obtenir un exemple de personnalisation de l'interface utilisateur de Microsoft Office Excel 2003, consultez Comment : créer par programme des menus Office.

Ajoutez le code suivant à la classe ThisDocument :

Remarque :

Déclarez vos variables de barre de commandes au niveau de la classe plutôt que dans la méthode où elles sont appelées. Cela garantit que les variables de barre de commandes restent dans la portée tant que l'application s'exécute. Sinon, l'élément est supprimé par garbage collection et votre code de gestionnaire d'événements ne s'exécute pas.

Exemple

' 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);
}

Voir aussi

Tâches

Comment : créer par programme des menus Office

Procédure pas à pas : création de menus contextuels pour les signets

Concepts

Personnalisation de l'interface utilisateur Office

Fonctionnement des paramètres optionnels dans les solutions Office