Comment : ajouter des barres d'outils personnalisées et des éléments de barre d'outils dans Outlook
Mise à jour : novembre 2007
S'applique à |
---|
Les informations dans cette rubrique s'appliquent uniquement aux projets Visual Studio Tools pour Office et versions de Microsoft Office spécifiés. Type de projet
Version de Microsoft Office
Pour plus d'informations, consultez Fonctionnalités disponibles par type d'application et de projet. |
Cet exemple crée une barre d'outils dans Microsoft Office Outlook. La barre d'outils, qui contient deux boutons apparaît en haut de l'application. Lorsque vous cliquez sur un bouton, le code affiche un message qui présente la légende de ce bouton.
Exemple
Dim newToolBar As Office.CommandBar
Dim firstButton As Office.CommandBarButton
Dim secondButton As Office.CommandBarButton
Dim selectExplorers As Outlook.Explorers
Private Sub ThisAddIn_Startup(ByVal sender As Object, _
ByVal e As System.EventArgs) Handles Me.Startup
selectExplorers = Me.Application.Explorers()
AddHandler selectExplorers.NewExplorer, AddressOf _
Me.NewExplorer_Event
AddToolbar()
End Sub
Private Sub NewExplorer_Event(ByVal new_Explorer _
As Outlook.Explorer)
new_Explorer.Activate()
newToolBar = Nothing
Call Me.AddToolbar()
End Sub
Private Sub AddToolbar()
Dim button_1 As Office.CommandBarButton
Dim button_2 As Office.CommandBarButton
If newToolBar Is Nothing Then
Dim cmdBars As Office.CommandBars = _
Me.Application.ActiveExplorer().CommandBars
newToolBar = cmdBars.Add("NewToolBar", _
Office.MsoBarPosition.msoBarTop, False, True)
End If
Try
button_1 = CType(newToolBar.Controls.Add(1), _
Office.CommandBarButton)
With button_1
.Style = Office.MsoButtonStyle.msoButtonCaption
.Caption = "Button 1"
.Tag = "Button1"
End With
If Me.firstButton Is Nothing Then
Me.firstButton = button_1
AddHandler firstButton.Click, AddressOf ButtonClick
End If
button_2 = CType(newToolBar.Controls.Add(1), _
Office.CommandBarButton)
With button_2
.Style = Office.MsoButtonStyle.msoButtonCaption
.Caption = "Button 2"
.Tag = "Button2"
End With
If Me.secondButton Is Nothing Then
Me.secondButton = button_2
AddHandler secondButton.Click, AddressOf ButtonClick
End If
newToolBar.Visible = True
Catch ex As Exception
MessageBox.Show(ex.Message)
End Try
End Sub
Private Sub ButtonClick(ByVal ctrl As Office.CommandBarButton, _
ByRef Cancel As Boolean)
MessageBox.Show("You clicked: " + ctrl.Caption)
End Sub
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);
}
Programmation fiable
Déclarez vos variables de barre de commandes au niveau de la classe. Si vous les déclarez à l'intérieur d'une méthode, elles dépasseront la portée dès que l'exécution de la méthode sera terminée et que le garbage collector sera en mesure de réaffecter la mémoire.
Voir aussi
Tâches
Comment : ajouter des menus personnalisés et des éléments de menu dans Outlook
Comment : créer des barres d'outils Office par programme
Comment : créer par programme des menus Office
Concepts
Vue d'ensemble du modèle d'objet Outlook