Extending Outlook Functionality with a .NET Add-In
In my last article on this topic, you learned how to interact with Outlook via your WinForms applications. In this article, you will explore how to extend the functionality of Outlook with your custom code. The business value here is clear: Give your users an environment in which they are comfortable to do tasks specific to their business needs. In addition to reduced training costs for new users, you also gain the benefits of a whole suite of functionality at your fingertips as opposed to the blank canvas. This article will walk through the creation of a simple add-in and will discuss a few tips and tricks to consider when writing your add-ins.
There are some useful Outlook Add-In templates in VSTO (Visual Studio Tools for Office); however, for this demonstration, you will stick with the plain Visual Studio Shared Add-In template. To begin, create a new project of "Shared Add-in" type:
Click here for a larger image.
Follow the wizard. For the "Application Host" section, choose Outlook. Notice here that you can write more generic add-ins to be used in multiple Office applications. For your purposes, you will just stick with Outlook.
Choose your own name and description, and make sure you check the "I would like my Add-in to load when the host application loads" option. You can choose whether or not your add-in should be installed for the current user or all users.
Visual Studio will create a solution with two projects. The first project will hold your add-in functionality. The second is a setup project that builds an MSI file you can use to install your add-in.
Now, you will notice that the first project contains a reference to the Microsoft.Office.Core library, which exposes functionality dealing with command bars, assistants, search, and so forth—basically the elements that are common among the Office applications. At this point, you also want to add a reference to the Outlook Object Library. You can follow the same instructions in the "Getting Started" section of my article "Interacting with Outlook from a WinForms Application." For this example, you will need to add one more reference to the System.Windows.Forms .NET library. The following uses directives in the Connect.cs file, which VS provides, and will make your code less verbose:
using Outlook = Microsoft.Office.Interop.Outlook; using Microsoft.Office.Core; using System.Reflection; using System.Windows.Forms;
Notice you will be using System.Reflection. Optional arguments permeate COM. The way around optional arguments in C# is to use the System.Reflection.Missing type, as you will see later in the code.
Creating Your First Outlook Add-In
The Connect class is the "in" for our add-in. It implements the Extensibility.IDTExtensibility2 interface. The required methods are stubbed out for you. The methods of interest for the purpose of this article are OnConnection and OnStartupComplete. You will use the OnConnection method to set up your class-level variables. The template provides two of these variables:
private object applicationObject; private object addInInstance;