03May

Building an Outlook 2010 Add In Using C#.NET With Visual Studio 2010

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.

Create new Outlook 2010 Add In template

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:

  1. Create a user interface for your custom task pane by adding Windows Forms controls to a UserControl object.
  2. 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. 

 Add user control to existing Add In project

Type “MyUserControl.cs” in the name box and create the user interface.

New Windows User Control

Add a label to the user control designer.

Windows user control user interface     

 

  1. 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;

  1. 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.

Task Pane

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.

Custom context menu

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.

Ribbon

Related

What are Frames? How to handle frames in Selenium WebDriver with C#?

IFrame (FullForm: Inline Frame) is an HTML document that is included in another HTML document and is...

Read More >

What is Synchronization? Handling Synchronization in Selenium WebDriver using C#:

Synchronization meaning: when two or more components involved to perform any action, we expect these...

Read More >

Sending Test reports by Email using Office 365, Gmail

Wouldn’t it be great if Test Report are sent automatically across team as soon the Test Execut...

Read More >

Token Based Authentication for Web API's

Securing ASP.NET Web API using Custom Token Based AuthenticationProviding a security to the Web API&...

Read More >

Extent Reports in Selenium CSharp (C#)

Reports play a fundamental role when it comes to TESTING. Tester can now  know the real-time r...

Read More >

How to Set Up Selenium WebDriver in Visual Studio Enterprise 2015?

Pre-requisite : Visual Studio Enterprise 2015 application should be installed into the system.Create...

Read More >

Custom Control in WPF

File Upload Custom Control in WPFThis article is about Custom control in WPF, Custom controls are al...

Read More >

ASP.NET: Audit Trail Implementation using Entity Framework

When you are working with certain projects which involves the Customer records, you might need to tr...

Read More >

Create Restful service using Web API

What is Asp.Net Web API?Asp.Net Web API is a framework for building HTTP services that can be consum...

Read More >

Dependency Injection on SignalR

                &nbs...

Read More >

Share

Post a Comment

  • Recent
  • Popular
  • Tag
Monthly Archive
Subscribe
Name

Contact Us
  • *
  • *