Building a VSTO Add-in for Office 2003 and Office 2007/2010/2013

There are a lot of folks out there still running Office 2003. My advice – upgrade. However, there are many vendors who do not have the luxury of being able to tell their users to simply upgrade. What I find a lot of times is that vendors are making an add-in for each version of Office they support. They do this for many different reasons, but the biggest is because in Office 2003 they need to add menus and in Office 2007+ they need to use Ribbon. And actually, Outlook 2007 only supports menus in the Explorer, so Outlook today is the most common scenario I encounter.

Bellow is a code sample for how to load CommandBars for Office 2003 (or Outlook 2007) and use Ribbons for Office 2007, Office 2010 and Office 2013:

private const string guidTag = "A766DECF-33AB-4D25-8091-C532A478E37B";
Office.CommandBarButton btn;

/// <summary>
/// ADD-IN STARTUP
/// Load the add-in. If we are on in Office 2003 (or Outlook 2007) we 
/// add a command bar button to the Tools menu. If it is Office 2010
/// or later, we will not do anything here as the Ribbon we have
/// attched will automatically load
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void ThisAddIn_Startup(object sender, System.EventArgs e)
{
    // enable the command bar button for version 2007 or earlier
    int version = getVersion();
    if ((Application.Name.ToLower().Contains("Outlook") && version <= 12) ||
        (version <= 11))
    {
        cleanToolbar();
        Office.CommandBar cbMenu = Application.ActiveExplorer().CommandBars.ActiveMenuBar;
        Office.CommandBarPopup popTools = (Office.CommandBarPopup)cbMenu.Controls["Tools"];
        btn = (Office.CommandBarButton)popTools.Controls.Add(
                    Office.MsoControlType.msoControlButton,
                    missing, missing, 1, true);
        btn.Caption = "&Do It...";
        btn.FaceId = 258;
        btn.Tag = guidTag;
        btn.Click += new Office._CommandBarButtonEvents_ClickEventHandler(btn_Click);
    }
}

/// <summary>
/// Get the version of the application
/// </summary>
/// <returns></returns>
private int getVersion()
{
    return int.Parse(Application.Version.Substring(0, Application.Version.IndexOf(".")));
}

/// <summary>
/// Command bar button was clicked - call out on action method
/// </summary>
/// <param name="Ctrl"></param>
/// <param name="CancelDefault"></param>
void btn_Click(Office.CommandBarButton Ctrl, ref bool CancelDefault)
{
    OnAction();
}

/// <summary>
/// This is where the work is done. When the user clicks on the ribbon
/// button or the command bar button, this method is called. 
/// </summary>
public void OnAction()
{
    // your code here
}
/// <summary>
/// Remove the toolbar in Outlook 2003/2007
/// </summary>
private void cleanToolbar()
{
    Office.CommandBar cbMenu = Application.ActiveExplorer().CommandBars.ActiveMenuBar;
    Office.CommandBarPopup popTools = (Office.CommandBarPopup)cbMenu.Controls["Tools"];
    foreach (Office.CommandBarControl ctl in popTools.Controls)
    {
        if (ctl.Tag == guidTag)
        {
            ctl.Delete();
            return;
        }
    }
}

/// <summary>
/// Cleanup - only if version 2007 or earlier
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void ThisAddIn_Shutdown(object sender, System.EventArgs e)
{
    if (getVersion() <= 12)
        cleanToolbar();
}

1 thought on “Building a VSTO Add-in for Office 2003 and Office 2007/2010/2013”

  1. To simplify my code I’m using partial methods.
    I have had two solutions one for VS2008(the last who supports office 2003) and one for VS2010/12. So I could share same code, but there are some files which doesn’t included into VS2008 and vice versa. This can not cause an error, because methods defined in these files declared as partial.

Leave a Reply to stouneCancel reply