Code Snippet
Outlook.Explorers myExplorers = null;
Office.CommandBar myCommandBar = null;
Office.CommandBarButton myCommandBarButton = null;
private void ThisAddIn_Startup(object sender, System.EventArgs e)
{
myExplorers = this.Application.Explorers;
myExplorers.NewExplorer += new Microsoft.Office.Interop.Outlook.ExplorersEvents_NewExplorerEventHandler(Explorers_NewExplorer);
if (this.Application.Explorers.Count == 1)
{
AddMenuButton();
}
}
void myCommandBarButton_Click(Microsoft.Office.Core.CommandBarButton Ctrl, ref bool CancelDefault)
{
MessageBox.Show("Test Button Clicked!");
}
void Explorers_NewExplorer(Microsoft.Office.Interop.Outlook.Explorer Explorer)
{
((Outlook.ExplorerEvents_10_Event)Explorer).Activate += new Microsoft.Office.Interop.Outlook.ExplorerEvents_10_ActivateEventHandler(ThisAddIn_Activate);
AddMenuButton();
}
void ThisAddIn_Activate()
{
AddMenuButton();
}
private void AddMenuButton()
{
try
{
myCommandBar = this.Application.ActiveExplorer().CommandBars["Menu Bar"];
myCommandBar.Reset();
Office.CommandBarControl myCommandBarControl = myCommandBar.Controls.Add(Office.MsoControlType.msoControlPopup, 1, missing, missing, true);
myCommandBarControl.Visible = true;
myCommandBarControl.Caption = "Test";
myCommandBarButton = ((Office.CommandBarPopup)myCommandBarControl).CommandBar.Controls.Add(Office.MsoControlType.msoControlButton, 1, "", 1, true) as Office.CommandBarButton;
myCommandBarButton.Caption = "test";
myCommandBarButton.Click += new Microsoft.Office.Core._CommandBarButtonEvents_ClickEventHandler(myCommandBarButton_Click);
}
catch (Exception ex)
{
}
}