2013-12-23

Excel Automation Add-In Won't Register

I spent a few hours trying to figure out why my C# Excel Automation Add-In wouldn't register. Every time I tried to add it, it complained that it wasn't an Automation Add-In. Everything looked right. All the code matched the examples from elsewhere, and the registry entries looked perfect, so why was it failing? I slept on it, and woke up with the answer. (Yes, I debug best in my sleep.) The name was too long!

It turns out that the entire name for the class must be 39 characters or less. This includes the namespace. The only punctuation allowed is a period. Thus

namespace Xoc.ReallyLongComponentName.ComInterface
{
    [ClassInterface(ClassInterfaceType.AutoDispatch)]
    public class AddInName
    {
        ...
    }
}
results in a class name in the IL (intermediate language) of Xoc.ReallyLongComponentName.ComInterface.AddInName, which is over the limit. The only way to solve this is to shorten the namespace.

namespace Xoc
{
    [ClassInterface(ClassInterfaceType.AutoDispatch)]
    public class AddInName
    {
        ...
    }
}

There is an attribute called ProgID, but it doesn't actually work. So if you just leave it with the long namespace and add the attribute, Excel doesn't recognize it correctly.

// This is insufficient
namespace Xoc.ReallyLongComponentName.ComInterface
{
    [ClassInterface(ClassInterfaceType.AutoDispatch)]
    [ProgID("Xoc.AddInName")]
    public class AddInName
    {
    ...
    }
}

How did I know? Well, I have run into stupid arbitrarily short name lengths before in Windows, and it connected for me that this might be one of those. After doing some research I found that the limitation is described in the ProgID attribute documentation.

Another problem I ran into...the AddIns.Add and AddIn2.Add methods were failing with the "Add method of the AddIn class failed" error. I was passing the file name of my add-in DLL. However, you can only pass the file names of XLAM add-ins using the file name. For DLL add-ins, you must pass the class name or ProgID, that you assigned using the techniques I described above. The DLL must already be registered with Windows, which should have been performed by the Install program for the DLL, or by using the regasm command line tool.

No comments :

Post a Comment

Note: Only a member of this blog may post a comment.