How to build an outlook 2010 Add In using C#.Net and Visual Studio 2010?
In this post, I’ll take you through the steps required to build an outlook 2010 plugin using C#.Net with visual studio 2010. This post includes creating custom task pane, adding a new context menu item with the outlook context menu and adding a ribbon bar that appears on the Home screen.
Let’s create the project. Open Visual Studio 2010, go to file menu select new project. In the Installed templates expand to Visual C# -> Office -> 2010 and select “Outlook 2010 Add-in” template. In the Name box, type MyFirstOutlookAddIn. Visual Studio creates the MyFirstOutlookAddIn project and opens the ThisAddIn code file in the editor.
By default, the ThisAddIn code file contains the following generated code:
- A partial definition of the ThisAddIn class. This class provides an entry point for your code and provides access to the object model of Outlook.
- The ThisAddIn_Startup and ThisAddIn_Shutdown event handlers. These event handlers are called when Outlook loads and unloads your add-in. Use these event handlers to initialize your add-in when it is loaded, and to clean up resources used by your add-in when it is unloaded.
Creating a Custom Task Pane:
You can create a basic custom task pane in two steps:
- Create a user interface for your custom task pane by adding Windows Forms controls to a UserControl object.
- Instantiate the custom task pane by passing the user control to the CustomTaskPaneCollection object in your add-in. This collection returns a new CustomTaskPane object that you can use to modify the appearance of the task pane and respond to user events.
All custom task panes that are created by using the Office development tools in Visual Studio contain a UserControl object. This user control provides the user interface of your custom task pane.
Adding User Control to the Outlook Add In project:
Right Click on the MyFirstOutlookAddIn project. Go to Add->User Control.
Type “MyUserControl.cs” in the name box and create the user interface.
Add a label to the user control designer.
- Add the following code to the ThisAddIn class. This code declares instances of MyUserControl and CustomTaskPane as members of the ThisAddIn class.
private MyUserControl myUserControl1;
private Microsoft.Office.Tools.CustomTaskPane myCustomTaskPane;
- Add the following code to the ThisAddIn_Startup event handler. This code creates a new CustomTaskPane by adding the MyUserControl object to the CustomTaskPanes collection. The code also displays the task pane.
private void ThisAddIn_Startup(object sender, System.EventArgs e) {
myUserControl1 = new MyUserControl();
myCustomTaskPane = this.CustomTaskPanes.Add(myUserControl1, “My Task Pane“);
myCustomTaskPane.Visible = true;
}
Press F5 to run the Add In.
Creating a Custom Context Menu:
Add the following code to the ThisAddIn_Startup event handler. This code creates a custom context menu by adding the event handler to the ItemContextMenuDisplay event. The code also displays the custom context menu item when right click the mail item.
private void ThisAddIn_Startup(object sender, System.EventArgs e){
this.Application.ItemContextMenuDisplay += new
Outlook.ApplicationEvents_11_ItemContextMenuDisplayEventHandler
(Application_ItemContextMenuDisplay);
}
private void Application_ItemContextMenuDisplay(Office.CommandBar CommandBar, Selection Selection) {
if (Selection[1] is MailItem) {
MailItem selectedMailItem = Selection[1] as MailItem;
this.CustomContextMenu(CommandBar, Selection);
}
}
This code also adds the event handler to the instance of the CommandBarButton click event which performs some action when clicking the menu item.
private void CustomContextMenu(Office.CommandBar CommandBar, Outlook.Selection Selection){
Office.CommandBarButton customContextMenuTag = (Office.CommandBarButton)
CommandBar.Controls.Add
(Office.MsoControlType.msoControlButton, Type.Missing, Type.Missing, Type.Missing, true);
customContextMenuTag.Click += new
Office._CommandBarButtonEvents_ClickEventHandler(customContextMenuTag_Click);
customContextMenuTag.Caption = “My Context Menu“;
customContextMenuTag.FaceId = 351; //displays the image for the menu item
customContextMenuTag.Style = Microsoft.Office.Core.MsoButtonStyle.msoButtonIconAndCaption;
}
private void customContextMenuTag_Click(Office.CommandBarButton Ctrl, ref bool CancelDefault) {
MessageBox.Show(“My Custom Menu Item Clicked“);
}
Press F5 to run the Add In.
Add a Ribbon in the Home Screen:
Firstly, to make life easier, download the Office 2010 Custom UI Schema, which will give you IntelliSense in the XML document.
Right click on MyFirstOutlookAddIn project, go to add new item select Ribbon (XML) and name it MyRibbon.cs.
The following xml is an example of the ribbon menu, which shows the structure of the ribbon. Replace the below xml with the MyRibbon.xml.
<?xml version=”1.0” encoding=”UTF-8“?>
<customUI xmlns=”http://schemas.microsoft.com/office/2009/07/customui” onLoad=”Ribbon_Load“>
<ribbon>
<tabs>
<tab id=”TabAddIns” label=”My Ribbon“>
<group id=”Group1” label=”Ribbon Options“>
<button id=”btnSettings” onAction=”btnSettings_Click” imageMso=”AdministrationHome” label=”Settings” size=”normal“/>
<button id=”btnHelp” onAction=”btnHelp_Click” imageMso=”Help” label=”Get Help and Support” size=”normal“/>
group>
<group id=”Group2“>
<button id=”BtnMy” imageMso=”HappyFace” onAction=”BtnMy_Click” label=”My Button” size=”large“/>
group>
tab>
tabs>
ribbon>
customUI>
You’ll notice we have added a button to the group that has an onAction event. Let’s hook that up now (as well as the button we added to our Ribbon). Open up MyRibbon.cs:
- At the top of MyRibbon.cs file – you will see some instructions that need to be followed – make sure you do point one by copying the code below into ThisAddIn.cs:
protected override Microsoft.Office.Core.IRibbonExtensibility CreateRibbonExtensibilityObject() {
return new MyRibbon();
}
- Now, create event handlers for each of the buttons we have created in MyRibbon .cs (the “onAction” attribute points to the method that will be called):
public void Ribbon_Load(Office.IRibbonUI ribbonUI) {
this.ribbon = ribbonUI;
}
public void btnSettings_Click(Office.IRibbonControl control) {
MessageBox.Show(“Settings Clicked“);
}
public void BtnMy_Click(Office.IRibbonControl control) {
MessageBox.Show(“My Button Clicked“);
}
public void btnHelp_Click(Office.IRibbonControl control) {
MessageBox.Show(“Help button Clicked“);
}
Press F5 to run the Add In.