VSTO: COM Exception 0x800A01A8


Problem

COM exceptions that give you just a HRESULT to describe an exception are terrible. During some VSTO development for Outlook 2007, I kept getting a COM exception with an HRESULT of 0x800A01A8, and eventually I found  the reason why I was receiving the exception. I though I’d share since little information is available on this particular exception.

Solution

The message for this COM exception should read something like this, “You are trying to access an object that does not exist“.

In my case I was trying to setup a toolbar and menu each time my add-in started up and then set some properties on them. On start-up I’d first check if the objects already existed and if so call their Delete() method. After that I had an IF-statement to check the objects for a null value and if there were indeed null, I’d recreate them. Then I’d try to set properties on the newly created objects, and that’s where I’d receive the COM exception. The problem was that after the Delete() method call, the objects will be removed from memory and Outlook, but my reference would still not be null.. it will just point to a non-existent object.. so my IF-statement would never fire and instead I’d end up trying to set properties on objects that no longer existed. So my quick fix was that right after the Delete call, I set my object references to null. Then my IF-statement would get called and the objects would be recreated and I could then set properties on them.

Hope that helps.

Example

public static Office.CommandBar CreateCommandBar(
    Outlook.Application application,
    string name,
    bool isVisible,
    bool overwriteExisting)
{
    Outlook.Explorer explorer = application.ActiveExplorer();
    if (explorer == null)
        return null;

    Office.CommandBar commandBar = FindCommandBar(application, name);
    if (commandBar != null && overwriteExisting)
    {
        commandBar.Delete();
        // Delete does not set reference to null.
        commandBar = null;
    }

    if (commandBar == null)
    {
        commandBar = explorer.CommandBars.Add(name, Office.MsoBarPosition.msoBarTop);
        if (commandBar == null)
            return null;
    }

    commandBar.Visible = isVisible;

    return commandBar;
}

public static Office.CommandBarPopup CreateMenu(
    Outlook.Application application,
    string name,
    string caption,
    bool isVisible,
    bool overwriteExisting)
{
    Outlook.Explorer explorer = application.ActiveExplorer();
    if (explorer == null)
        return null;

    Office.CommandBar menuBar = explorer.CommandBars.ActiveMenuBar;
    if (menuBar == null)
        return null;

    Office.CommandBarPopup menu = menuBar.FindControl(Office.MsoControlType.msoControlPopup, Tag: name, Recursive: true)
        as Office.CommandBarPopup;
    if (menu != null && overwriteExisting)
    {
        menu.Delete();
        // Delete does not set reference to null.
        menu = null;
    }

    if (menu == null)
    {
        menu = menuBar.Controls.Add(Office.MsoControlType.msoControlPopup) as Office.CommandBarPopup;
        if (menu == null)
            return null;
    }

    menu.Caption = caption;
    menu.Tag = name;
    menu.Visible = isVisible;

    return menu;
}

public static Office.CommandBar FindCommandBar(Outlook.Application application, string name)
{
    Outlook.Explorer explorer = application.ActiveExplorer();
    if (explorer == null)
        return null;

    return (from Office.CommandBar commandBar in explorer.CommandBars
            where commandBar.Name == name
            select commandBar).FirstOrDefault();
}

, , ,

  1. #1 by Igor on October 28, 2011 - 5:54 am

    Thank you, this helped me to solve very unpleasant bug :)

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: