Writing an Outlook Add-in

Yesterday I talked about personalizing Outlook Today to make it look more sexy. One of the things I had on my Outlook Today, was my picture with a direct link to my contact details. This link looked like this:

[html] Contact Details [/html]

As you can see, I'm pointing to 0000000031490E5EAED5DA4AAB47FD1B9F1E6BA7E43D2D00. This is the name Outlook internally assigned to my contact details, but how did I find it?

To figure this out, I wrote an Outlook Add-in which simply gives me the internal name of any selected item, be it a mail, appointment, contact or task, as shown here:

GuidFinder Result

Creating this Add-in was quite easy with Visual Studio 2008. I started by creating a new project of the type Office - 2007 - Outlook Add-in, which results in one class presenting you two important methods:

[csharp] private void ThisAddIn_Startup(object sender, System.EventArgs e) { }

private void ThisAddIn_Shutdown(object sender, System.EventArgs e) { } [/csharp]

Using the following MSDN resources, I quickly hacked a small Outlook Add-in together, which would give me an additional menu item. GuidFinder Menu Item

To add a new button, I changed the code from the article a bit, to look as follows:

[csharp]

#region Add menu item private void AddMenuBar() { try { // Get the current menu bar. this.menuBar = this.Application.ActiveExplorer().CommandBars.ActiveMenuBar;

    // Add a new item to the menu bar.
    this.newMenuBar = (CommandBarPopup)menuBar.Controls.Add(
                       MsoControlType.msoControlPopup, missing,
                       missing, missing, false);

    // Add the menu bar if it doesn't exist.
    if (this.newMenuBar != null)
    {
        this.newMenuBar.Caption = "GuidFinder";
        this.newMenuBar.Tag = this.menuTag;

        // Add a new menu item to the menu.
        this.getGuidButton = (CommandBarButton)newMenuBar.Controls.Add(
                              MsoControlType.msoControlButton, missing,
                              missing, 1, true);

        // Layout the menu item.
        this.getGuidButton.Style = MsoButtonStyle.msoButtonIconAndCaption;
        this.getGuidButton.Caption = "Get GUID";
        this.getGuidButton.FaceId = 25; // Looking Glass icon
        this.getGuidButton.Click += new _CommandBarButtonEvents_ClickEventHandler(this.getGuid_Click);

        // Make our result visible.
        this.newMenuBar.Visible = true;
    }
}
catch (System.Exception ex)
{
    MessageBox.Show("Error: " + ex.Message.ToString(), "Error Message Box", MessageBoxButtons.OK, MessageBoxIcon.Error);
}

}

#endregion [/csharp]

The FaceId property defines the icon which will be displayed next to your menu item. To get a list of possible icons, have a look at these images.

When you run the Add-in at this stage, buttons will keep on adding themselves to Outlook every time you run it. Therefore we need to write some code which removes the button in case it already exists:

[csharp]

#region Remove Menu item private void RemoveMenubar() { // If the menu already exists, remove it. try { CommandBarPopup foundMenu = (CommandBarPopup) this.Application.ActiveExplorer().CommandBars.ActiveMenuBar. FindControl(MsoControlType.msoControlPopup, missing, menuTag, true, true);

    if (foundMenu != null)
    {
        foundMenu.Delete(true);
    }
}
catch (System.Exception ex)
{
    MessageBox.Show("Error: " + ex.Message.ToString(), "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
}

}

#endregion [/csharp]

Clicking on the Get GUID button should give us the internal name of the item, for which we use the following code to display it:

[csharp]

#region Display the Guid private void getGuid_Click(CommandBarButton ctrl, ref bool cancel) { try { // Get the selected item in Outlook and determine its type. Selection outlookSelection = this.Application.ActiveExplorer().Selection;

    if (outlookSelection.Count > 0)
    {
        string itemGuid = string.Empty;
        object selectedItem = outlookSelection[1];

        if (selectedItem is MailItem)
        {
            MailItem mailItem = (selectedItem as MailItem);
            itemGuid = mailItem.EntryID;
        }
        else if (selectedItem is ContactItem)
        {
            ContactItem contactItem = (selectedItem as ContactItem);
            itemGuid = contactItem.EntryID;
        }
        else if (selectedItem is AppointmentItem)
        {
            AppointmentItem apptItem = (selectedItem as AppointmentItem);
            itemGuid = apptItem.EntryID;
        }
        else if (selectedItem is TaskItem)
        {
            TaskItem taskItem = (selectedItem as TaskItem);
            itemGuid = taskItem.EntryID;
        }
        else if (selectedItem is MeetingItem)
        {
            MeetingItem meetingItem = (selectedItem as MeetingItem);
            itemGuid = meetingItem.EntryID;
        }

        if (itemGuid != String.Empty)
        {
            MessageBox.Show(String.Format("The GUID of this item is {0}", itemGuid), "Guid", MessageBoxButtons.OK, MessageBoxIcon.Information);
        }
        else
        {
            MessageBox.Show("The item type could not be determined.", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
        }
    }

}
catch (System.Exception ex)
{
    MessageBox.Show("Error: " + ex.Message.ToString(), "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
}

}

#endregion [/csharp]

Wiring up everything in the end, is as simple as first checking if our menu already exists and removing it, to afterwards add it to Outlook again.

[csharp] private void ThisAddIn_Startup(object sender, System.EventArgs e) { this.RemoveMenubar(); this.AddMenuBar(); } [/csharp]

Once again, I Outlook 2007 and works with <a href="http://www.microsoft.com/outlook/ "Outlook Home Page"). This small GuidFinder Add-in is a nice start to discover the full potential of Outlook Add-ins.

If you aren't interested in the code, but would like to have this Add-in installed in Outlook 2007, run the ClickOnce installer to deploy it on your computer. This installer will get all prerequisites needed to run the Add-in (.NET 3.5) and the Add-in itself. If you get an error about an untrusted certificate, add wiki.cumps.be to your Trusted Sites zone.

I belief this is a very powerful technology to use inside a company and provide added value to your employees, making them more productive through useful add-ins. Thanks to the ClickOnce technology it's easy to deploy add-ins to clients and keep them updated.

What do you think about .NET Outlook Add-ins and ClickOnce?