Emil Hunefalk

Programming Outlook 2007 add-ins with Visual C# 2008 Express

Posted on | September 17, 2009 | 7 Comments

I recently started looking in to creating add-ins for Outlook, as part of a possible project to extend the functionality of MS Outlook 2007 – by far the most popular e-mail client in the business world (when including browser ‘clients’ such as Hotmail and Gmail, Outlook still had 36% market share in the most recent statistics I could find, with Hotmail second at 33%, Apple Mail 5th at 4% while my own favorite Thunderbird is 7th on 2.4% as seen on FingerprintApp.com, which shows similar MS dominance in the consumer market).

As a first part of planning for development on the project, I decided to find out if I could use free tools to create add-ins for Outlook, and started with Visual C# 2008 Express – a free sibling to the popular but costly Visual Studio 2008 (of course you get much more for the paid version, which also made even the trial version too big a package for me to download on the road with mobile broadband), for which you can use for example Visual Studio Tools for Office (VSTO) and Add-in Express for quick development. Furthermore, considering I was new to development for MS Office, I believed that having fewer advanced tools would give the chance to learn more about this type of development, thereby giving skills I wouldn’t find by going the easiest way, and also leading to fewer coding mistakes down the line…

Whether I’m right or wrong in the choice is for each to decide on his/her own, and either way I found a way to create Outlook add-ins with free tools. I started off by following two different blog posts (see references at end of post) which were both written for older versions of Visual C# Express and Outlook. Since the posts were a bit dated (or perhaps for other reasons), the described development was a incomplete, and further investigation had to be made. To make a long story short, what I needed to do was to create a Class project, add references to

  • Microsoft Office 12.0 Object Library (COM, shows up as Microsoft.Office.Core)
  • Microsoft Outlook 12.0 Object Library (COM, Microsoft.Office.Interop.Outlook)
  • System.Windows.Forms (.NET, keeps same name)

The code I ended up with for a simple “Hello World” was a as follows:

using System;
using System.Collections.Generic;
using System.Text;
using System.Windows.Forms;
using Extensibility;
using Microsoft.Office.Core;
using Microsoft.Office.Interop.Outlook;

namespace OutlookAddinSample
{
    public class Connect : Object, IDTExtensibility2
    {
        private Application applicationObject;
        private object addInInstance;
        private CommandBarButton toolbarButton;
        Explorer currentExplorer = null;

        public void OnConnection(object application,
            ext_ConnectMode connectMode, object addInInst,
            ref Array custom)
        {
            applicationObject = 
                (Microsoft.Office.Interop.Outlook.Application)application;
            addInInstance = addInInst;
            if (connectMode != Extensibility.ext_ConnectMode.ext_cm_Startup)
            {
                OnStartupComplete(ref custom);
            }
        }

        public void OnDisconnection(ext_DisconnectMode
            disconnectMode,
            ref Array custom)
        {
            if (disconnectMode != 
                Extensibility.ext_DisconnectMode.ext_dm_HostShutdown)
            {
                OnBeginShutdown(ref custom);
            }
            applicationObject = null;
        }

        public void OnAddInsUpdate(ref Array custom)
        {
        }

        public void OnStartupComplete(ref Array custom)
        {
            CommandBars commandBars = 
                applicationObject.ActiveExplorer().CommandBars;
            try
            {
                this.toolbarButton = 
                    (CommandBarButton)
                    commandBars["Standard"].Controls["Hello"];
            }
            catch (System.Exception)
            {
                this.toolbarButton = 
                    (CommandBarButton)commandBars["Standard"].Controls.Add(1, 
                        System.Reflection.Missing.Value, 
                        System.Reflection.Missing.Value, 
                        System.Reflection.Missing.Value, 
                        System.Reflection.Missing.Value);
                this.toolbarButton.Caption = "Hello";
                this.toolbarButton.Style = MsoButtonStyle.msoButtonCaption;
            }
            this.toolbarButton.Tag = "Hello Button";
            this.toolbarButton.OnAction = "!<MyAddin1.Connect>";
            this.toolbarButton.Visible = true;
            this.toolbarButton.Click += 
                new Microsoft.Office.Core._CommandBarButtonEvents_ClickEventHandler(
                    this.OnToolbarButtonClick);
        }

        public void OnBeginShutdown(ref Array custom)
        {
            this.toolbarButton.Delete(System.Reflection.Missing.Value);
            this.toolbarButton = null;
        }

        private void OnToolbarButtonClick(CommandBarButton cmdBarbutton, ref bool cancel)
        {
            MessageBox.Show("Hello World", "My Addin");
        }
    }
}

Sorry about the code formatting, but hopefully it helps someone on getting started. After creating your project with the code for your Outlook add-in, you need to make it visible for the COM and make it strongly typed by signing. The signing is done simply by going to the project Properties (right-click on your project in the Solution Explorer) and then to Signing, where tick Sign the assembly and make a new key file (assuming you don’t have one already). For COM visibility, you go to the Application tab in the properties, click the Assembly Information button and tick the checkbox “Make assembly COM visible”. To make mine work I also took a couple of steps which were not recommended in the post I found information from, so try without the following first:

Under the Build tab, check “Register for COM interop”

Furthermore, the article I followed recommended adding to the registry (start->run->”regedit” on Windows Vista) at the location HKEYLOCALMACHINE\SOFTWARE\Microsoft\Office\Outlook\Addins which didn’t work for me, and I instead added lines at HKEYCURRENTUSER\Software\Microsoft\Outlook\Addin. The added keys under () OutlookAddin.Connect (in my example with namespace OutlookAddin and class Connect; i.e. namespace.mainClass) were

  • (STRING) “Description” = “Mail@rchive for Outlook”
  • (STRING) “FriendlyName” = “Mail@rchive for Outlook”
  • (DWORD) “LoadBehavior” = 00000003

Finally, to make a readable library of your add-in, and to register the installation folder to be used, copy the regasm.exe file (I found mine under C:\Windows\Microsoft.NET\Framework\v2.0.50727, although .NET 3.5 is used in the project) to your [project path]/bin/release, navigate to that folder in your command prompt (start->run->cmd) and run:

  • regasm OutlookAddin.dll/tlb:OutlookAddin.tlb /codebase

(this of course only if your dll is named OutlookAddin.dll and you want the tlb to have the name OutlookAddin.tlb)

Following this procedure should add a button to your toolbar saying “Hello”, and when clicking it should pop up a “Hello World” form. If you have any questions or can’t make it work feel free to post a comment.

References:


Bookmark and Share

Comments

7 Responses to “Programming Outlook 2007 add-ins with Visual C# 2008 Express”

  1. Robert
    October 27th, 2009 @ 15:11 pm

    Do need to have Office 2007 installed to test and run your addin?

  2. EH
    October 27th, 2009 @ 15:22 pm

    Hi Robert,

    You need to have Outlook 2007 installed to properly run the add-in I am currently working on (started with the code seen in the post – which of course has later been heavily modified and added to, for example by replacing the Outlook 2003-compatible CommandBarButton with a Ribbon group).

    However, the code in this post should also work with at least Outlook 2003 and 2010. It does work with the Outlook 2007 trial version, which has been used to test it on a local machine here. I don’t know if it works with for example free Outlook Express, as I am currently only developing it for Outlook 2007.

  3. Robert
    October 28th, 2009 @ 13:14 pm

    I install the Primary Interop Assemblies for Microsoft Office 2007. I was able to add the reference to the Microsoft.Office.Core but there was no reference for Microsoft.Office.Interop.Outlook. What am I doing wrong. Do i need to install the trial version of office 2007?

  4. EH
    October 28th, 2009 @ 13:19 pm

    Microsoft.Office.Interop.Outlook comes with the Office 2007 installation – as far as I know you can’t get it separate.

  5. Robert
    October 28th, 2009 @ 15:18 pm

    Do I need to install outlook 2007 to get access to the IDTExensibility Assemblies? How do I add this reference so I can add the line:

    using Extensibility;

    To my code.

  6. EH
    October 28th, 2009 @ 15:27 pm

    You can add a reference to the Extensibility under the .NET tab, with the name “extensibility”. In other words, it’s not under the COM tab with the Office references, and only called extensibility, without “IDT” in the start. There’s some information from MS at http://support.microsoft.com/kb/302901

  7. shivranjani
    January 19th, 2010 @ 12:44 pm

    thanks a lot…Your post really helped to solve my problem.I googled for almost four days, could not find anything.My problem was while adding the key under HKEYLOCALMACHINESOFTWAREMicrosoftOfficeOutlookAddins ,I just added namespace of project not with class namespace.
    Once again thanks a lot.

Leave a Reply





  • LinkedIn

    If you want to see my LinkedIn profile, click on this button:

    Emil Hunefalk
  • Find & Follow

    hunefalk StumbleUponhunefalk Twitterhunefalk Flickrhunefalk Delicioushunefalk Facebookhunefalk Friendfeedhunefalk LinkedInhunefalk YouTubehunefalk Last.fmhunefalk MyBlogLoghunefalk Google Readerhunefalk Picasawebhunefalk Facebook Profile
  • Comments

  • Tag Cloud

  • addin
  • development
  • mail-client
  • ms-outlook
  • programming
  • visual-csharp