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!
4 comments:
Thanks for this wonderful blog dude. I searched for a long time in the internet for a simple sample outlook addin project. But could not find one. This is what i was looking for! Thanks Again!!!
I noticed numerous programs for recovering emails on the I-net blogs and forums. But no one of theirs couldn't help me yesterday evening, when I had 11 corrupted emails from my little brother. After that I set an objective for myself. Or more exactly I wanted to find a good tool. And I found it - export outlook ost file. It relieved my easy and quickly.
Worked for me, thanks.
Excellent post. have you posted another one ? do you recommend some book or any tutorial for a beginner like me ?
Post a Comment