Create an Automation Add-In for Excel using .NET






4.64/5 (32 votes)
Jul 19, 2004
3 min read

614603
Describes how to create an automation add-in for Excel using .NET.
Introduction
Excel versions 2002 (XP) and 2003 introduce the concept of an Automation Add-In. Automation Add-Ins allow public functions in COM libraries to act as User-Defined Functions (UDFs) in Excel, thus to be referenced directly from cell formulae.
This article provides a detailed walk-through of how to create an Automation Add-In for Excel using Visual Studio .NET.
Background
Some relevant knowledge base articles that discuss Automation Add-Ins are:
- Q291392 - INFO: Excel COM Add-ins and Automation Add-ins.
- Q285337 - How To Create a Visual Basic Automation Add-in for Excel Worksheet Functions.
- Q278328 - XL2002: How to Mark an Automation Add-In Function as Volatile.
A commercial library that presents equivalent (and more) functionality, and makes this all very easy is ManagedXLL. However, this library is quite expensive and requires run-time licenses to distribute user code. It uses the native XLL API for creating an add-in to Excel, and thus also supports older versions of Excel.
The exact requirements for creating a COM server that can be used as an Automation Add-In are poorly documented, complicated by the fact that the default options in Visual Basic 6.0 seem to work perfectly.
When creating a COM library using .NET, an attempt to add the library as an Automation Add-In in Excel causes the error: “The file you selected does not contain a new Automation Server, or you do not have sufficient privileges to register the Automation Server”.
Here, I describe how to create an Automation Add-In in .NET, using C#. The techniques should apply to any .NET language.
There seem to be three tricks for implementing an Automation Add-In for Excel using .NET:
-
The library needs to be registered for use through COM. This can be done by
marking the project to ‘Register for COM Interop’ or by manual
registration using
RegAsm.exe
. -
The
‘Programmable’
registry key needs to be added in the registry, underHKCR\CLSID\{xxx}\
. This can be automated by adding appropriateComRegisterFunction
methods to the class. -
The class needs to be marked with the
ClassInterface
attribute, with valueClassInterfaceType.AutoDual
(explicit interface implementations can work too). The default class interface that is generated is a dispatch interface - Excel seems to ignore the dispatch interface for Automation Add-Ins.
Walk-through
- Create the library:
- Create a new C# Class Library Project, called NAddIn.
- Select the project's properties; under ‘Configuration Properties’, ‘Build’, set ‘Register for COM Interop’ to True.
-
Rename the class, add a namespace declaration, set the
ClassInterface
attribute of the class, and add a function to be used from Excel:using System; using System.Runtime.InteropServices; namespace NAddIn { [ClassInterface(ClassInterfaceType.AutoDual)] public class Functions { public Functions() { } public double Add2(double v1, double v2) { return v1 + v2; } [ComRegisterFunctionAttribute] public static void RegisterFunction(Type t) { Microsoft.Win32.Registry.ClassesRoot.CreateSubKey( "CLSID\\{" + t.GUID.ToString().ToUpper() + "}\\Programmable"); } [ComUnregisterFunctionAttribute] public static void UnregisterFunction(Type t) { Microsoft.Win32.Registry.ClassesRoot.DeleteSubKey( "CLSID\\{" + t.GUID.ToString().ToUpper() + "}\\Programmable"); } } }
- Build the NAddIn project to create bin\debug\NAddIn.dll.
-
Test the Add-In in Excel:
- Open a new workbook in Excel.
- Select Tools, Add-Ins, Automation.
-
NAddIn.Functions
should be listed - select it. OK. -
In a cell, type
=Add2(3,4)
- The cell should display 7.
regasm
with the
/codebase
flag (typically as c:\WINDOWS\Microsoft.NET\Framework\v1.1.4322\RegAsm
/codebase NAddIn.dll
).
You will get a warning about the assembly being unsigned - you can ignore this
(or sign the assembly as documented).
History
- Initial version - 19 July 2004.
- Removed type library embedding instructions - 19 July 2004.
-
Added
ComRegisterFunction
bits to automate registry changes - 30 July 2004.