Writing an Outlook Add-in with C# (Part 1)

One of the more fun things to do as a developer is to interface with Microsoft Office.  That “fun” word is a little bit tongue-in-cheek, because sometimes it is anything but fun to write apps that talk to Office. 

I had a recent experience writing an Outlook add-in that hopefully will save you a lot of time and hair pulling (perhaps a bit of head banging also).   ( What is an Outlook Add-in? 

I’m also choosing NOT to use VSTO tools for Office, because there are quite a few docs on how to do that already.  This tutorial will be about writing an Outlook Add-in with “raw” C#.  Contrary to what the Microsoft docs might lead you to believe, it is possible!

First, fire up Visual Studio (I’m using 2005) and choose File/New Project…  In the ensuing dialog,  find “Other Project Types” in the tree control and open it up.  Pick “Extensibility” in the tree and “Shared Add-in” from the list on the right.  

Give your project a name (”MyAddin” is good enough) and hit OK.  This will bring up the Add-in Wizard.  Hit “Next” and on the next dialog, pick “Create an Add-in  using Visual C#.”   Hit “Next.”

The next step in the wizard allows you to select what applications you want to target.  In this case, un-select all except Microsoft Outlook.  Click “Next.”

In the next dialog give your Add-in a name and a description.  The name is what will show up in the Outlook Add-in page (described later) so you can pick your Add-in to load. 

In the next dialog you can choose whether or not to load your Add-in when the host application (Outlook) loads.  Check this box.  The other check box on this screen asks whether this Add-in should be available to all users of the computer it was installed on.   Important: DO NOT check this box. Even though the Microsoft docs say to do so.   The Add-in will NOT work right if you check this box.   The reason has to do with where your Add-in gets put in the Windows registry and where Outlook tries to find Add-ins to load. 

Click “Next” then “Finish” on the last dialog. 

Visual Studio will create a project for you with several files, the most important of which is “connect.cs”   This is an interop class (yes, Office is still COM based) that implements an interface called IDTExtensibility2, which is the interface Outlook talks to in its Add-ins. 

 There are several methods here that get called at various times by Outlook.   Just briefly they are:

OnConnection( )

This gets called when the Add-in is being loaded by Outlook.  It is passed several parameters - The interesting ones are: The Application object, and an instance of the Add-in (as objects). 

OnDisconnection( )

Called when the Add-in is being unloaded.

OnAddInsUpdate ( )

Not particularly interesting unless you care when the collection of Outlook Add-ins is changing.

OnStartupComplete ( )

 Notification that Outlook has completed loading.  Might be useful in some cases.

OnBeginShutdown ( )

Notification that Outlook is being unloaded.  Might also be useful in some cases.

The most interesting of the methods is OnConnection and it is where we will begin our customization of Outlook. 

More in Part 2…

One Response to “Writing an Outlook Add-in with C# (Part 1)”

  1. […] This article is going to carry on some ideas from earlier posts. Those posts dealt with how to customize an Outlook Contact by adding a custom tab. If you missed the earlier information, you can catch up on it here: http://www.midniteblog.com/2007/04/12/writing-an-outlook-add-in/. […]

Leave a Reply

You must be logged in to post a comment.