Tuesday, July 21, 2009

Create Outlook 2007 Add-in Button using VSTO

While searching for "How to create simple outlook add-in using VSTO with single button" on net, I could
not find any sample.
One good link I found is Creating Add ins For Outlook 2007

Jeff has explained how to add menu bar to Outlook 2007 nicely here. I just extended his code to add simple
add-in button.

Start Visual Studio, create a new project. In the Project Types, select Visual C# -> Office -> 2007.
In the right hand pane, choose Outlook 2007 Add-in.





Add few properties to class file generated by VSTO



private Outlook.Explorer explorer;
private Office.CommandBar bar;
private Office.CommandBarButton buttonOne;



Add function to add a new button



private void AddButton()
{
try
{
//Define the existent Menu Bar
explorer = this.Application.ActiveExplorer();

bar = explorer.CommandBars.Add("Hello World Bar", Microsoft.Office.Core.MsoBarPosition.msoBarTop,
missing, false);
bar.Visible = true;

buttonOne = (Office.CommandBarButton)bar.Controls.Add(Office.MsoControlType.msoControlButton, missing,
missing, missing, false);

buttonOne.Caption = "Hello World";
buttonOne.Tag = "Hello World Bar.buttonOne";
buttonOne.Style = Office.MsoButtonStyle.msoButtonCaption;

//Assign event handler for button
buttonOne.Click += new Office._CommandBarButtonEvents_ClickEventHandler(buttonOne_Click);
}
catch (Exception ex)
{
//This MessageBox is visible if there is an error
System.Windows.Forms.MessageBox.Show("Error: " + ex.Message.ToString(), "Error Message Box", MessageBoxButtons.OK,
MessageBoxIcon.Exclamation);
}
}



Write a event handler function



private void buttonOne_Click(Office.CommandBarButton ctrl, ref bool cancel)
{
System.Windows.Forms.MessageBox.Show("Hello World");
}



Add call to "AddButton" function in "ThisAddIn_Startup" function



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



If you execute this code multiple times then it will add your add-in to outlook command bars multiple times. To avoid that just add function to Remove the add-in on start up.

Write one more function to remove the add-in on start up.




#region Remove Button from Outlook's command bar
private void RemoveButton()
{
//If the button already exists, remove it.
try
{
Office.CommandBarButton foundMenu = (Office.CommandBarButton)
this.Application.ActiveExplorer().CommandBars.FindControl(
Office.MsoControlType.msoControlButton,
missing, "Hello World Bar.buttonOne", true);
if (foundMenu != null)
{
foundMenu.Delete(true);
}
foreach (Office.CommandBar bar in this.Application.ActiveExplorer().CommandBars)
{
if (bar.Name.Equals("Hello World Bar"))
{
bar.Delete();
}
}
}
catch (Exception ex)
{
System.Windows.Forms.MessageBox.Show(ex.Message);
}
}
#endregion



Now add call to "RemoveButton" in "ThisAddIn_Startup" function before call to "AddButton"



private void ThisAddIn_Startup(object sender, System.EventArgs e)
{
//Search the menu and delete it if found
RemoveButton();
AddButton();
}



Now execute the code. This will start Outlook 2007 and add one command bar button to Outlook toolbar.

Happy Coding!