Click here to Skip to main content
16,009,068 members
Articles / Programming Languages / C#
Article

Create an Automation Add-In for Excel using .NET

Rate me:
Please Sign up or sign in to vote.
4.64/5 (36 votes)
3 Aug 20043 min read 613.3K   84   125
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:

  1. 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.
  2. The ‘Programmable’ registry key needs to be added in the registry, under HKCR\CLSID\{xxx}\. This can be automated by adding appropriate ComRegisterFunction methods to the class.
  3. The class needs to be marked with the ClassInterface attribute, with value ClassInterfaceType.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

  1. Create the library:
    1. Create a new C# Class Library Project, called NAddIn.
    2. Select the project's properties; under ‘Configuration Properties’, ‘Build’, set ‘Register for COM Interop’ to True.
    3. Rename the class, add a namespace declaration, set the ClassInterface attribute of the class, and add a function to be used from Excel:
      C#
      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");
              }
          }
      }
    4. Build the NAddIn project to create bin\debug\NAddIn.dll.
  2. Test the Add-In in Excel:
    1. Open a new workbook in Excel.
    2. Select Tools, Add-Ins, Automation.
    3. NAddIn.Functions should be listed - select it. OK.
    4. In a cell, type =Add2(3,4)
    5. The cell should display 7.
To register the .dll after moving it, run 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

  1. Initial version - 19 July 2004.
  2. Removed type library embedding instructions - 19 July 2004.
  3. Added ComRegisterFunction bits to automate registry changes - 30 July 2004.

License

This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here


Written By
Web Developer
South Africa South Africa
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.

Comments and Discussions

 
GeneralRe: I can't see it in automation Please help Pin
Zully Ramos29-Feb-08 8:59
Zully Ramos29-Feb-08 8:59 
GeneralRe: I can't see it in automation Please help Pin
Jeltz126-May-06 20:41
Jeltz126-May-06 20:41 
GeneralExcelDna - an alternative approach Pin
Govert van Drimmelen24-Jan-06 0:13
Govert van Drimmelen24-Jan-06 0:13 
GeneralRange Reading value Pin
germoz28-Sep-05 8:56
germoz28-Sep-05 8:56 
GeneralRe: Range Reading value Pin
Govert van Drimmelen5-Oct-05 5:17
Govert van Drimmelen5-Oct-05 5:17 
GeneralReturning a struct Pin
Member 193984316-Aug-05 11:20
Member 193984316-Aug-05 11:20 
GeneralWould like to put help in Addin Pin
Fardoche616-Mar-05 23:52
Fardoche616-Mar-05 23:52 
GeneralRe: Would like to put help in Addin Pin
OhioYooper14-Apr-05 5:46
OhioYooper14-Apr-05 5:46 
I've been trynig to get the same thing for some time.
Here's what I've found (so far):

1) it can't be done
From http://support.microsoft.com/kb/285337/
Each Automation Add-in has its own category in the Excel Function Wizard. The category name is the ProgID for the Add-in; you cannot specify a different category name for Automation Add-in functions. Additionally, there is no way to specify function descriptions, argument descriptions, or help for Automation Add-in functions in the Function Wizard.

2) If it could work somehow, contextual help has to be in Winhelp format (see http://msdn.microsoft.com/library/.../deconcreatinghelpfiletousewithofficesolution.asp[^]).

3) Solution? Make custom help available through the Answer Wizard. This requires help in HTML format, and building an Answer Wizard file. Still not contextual, but at least the help will be there for the user, they just need to launch help on their own to get it (and search for it in the Answer Wizard or with keywords).

4) Another Solution? Use very long, very descriptive argument names (e.g Function Density(TemperatureInDegreeF, String_Water_or_Gasoline)) At least these show up in the Insert Function wizard. One caveat: Optional arguments will appear, but will not have any indication they are optional.

One other odd-ball thing: I have built HTML help using the MS Office style sheet (office.css) so my function help would look just like Excel's functions. While it does when launching the help file by itself, it displays with other fonts, and javascript doesn't work, when launched from the topics list (either in the Answer Wizard panel, or the Index panel in Excel help). Oddly, when jumping to my topics from my topics within Excel help, the topics display correctly.

Matt

Update: I solved the odd-ball thing.
in my HTML topics, I had referenced the stylesheet as HREF="office10.css". Changing that to HREF="ms-its:C:\Program
Files\Microsoft Office\Office10\1033\xlmain10.­chm::/html/office10.css" fixed the problem. Also all the Javascript src's should be similarly modified. If you extract the individual help from XLMAIN10.CHM, you will have to modify them as they will have the shorter references.

Matt
GeneralApplication.Volatile Pin
Member 184141110-Mar-05 8:21
Member 184141110-Mar-05 8:21 
QuestionHow to use the ParamArray with Automation? Pin
luissimoes28-Feb-05 5:35
luissimoes28-Feb-05 5:35 
AnswerRe: How to use the ParamArray with Automation? Pin
Govert van Drimmelen3-Mar-05 1:06
Govert van Drimmelen3-Mar-05 1:06 
GeneralRe: How to use the ParamArray with Automation? Pin
abhiram_nayan6-Jan-09 20:05
abhiram_nayan6-Jan-09 20:05 
GeneralRe: How to use the ParamArray with Automation? Pin
abhiram_nayan6-Jan-09 20:55
abhiram_nayan6-Jan-09 20:55 
GeneralRe: How to use the ParamArray with Automation? Pin
abhiram_nayan7-Jan-09 1:47
abhiram_nayan7-Jan-09 1:47 
GeneralVisual Basic.net Pin
wabber6-Feb-05 10:07
susswabber6-Feb-05 10:07 
GeneralRe: Visual Basic.net Pin
Christian Graus6-Feb-05 10:15
protectorChristian Graus6-Feb-05 10:15 
GeneralRe: Visual Basic.net Pin
Anonymous6-Feb-05 22:02
Anonymous6-Feb-05 22:02 
GeneralRe: Visual Basic.net Pin
Govert van Drimmelen7-Feb-05 1:24
Govert van Drimmelen7-Feb-05 1:24 
GeneralRe: Visual Basic.net Pin
Anonymous7-Feb-05 10:16
Anonymous7-Feb-05 10:16 
GeneralAdd2(3,4) works but not Add2(A1,B1) Pin
rotarinn9-Jan-05 5:48
rotarinn9-Jan-05 5:48 
GeneralRe: Add2(3,4) works but not Add2(A1,B1) Pin
Govert van Drimmelen18-Jan-05 0:17
Govert van Drimmelen18-Jan-05 0:17 
GeneralRe: Add2(3,4) works but not Add2(A1,B1) Pin
nulken14-May-06 22:41
nulken14-May-06 22:41 
GeneralReturning an Array Pin
firmwaredsp31-Dec-04 19:26
firmwaredsp31-Dec-04 19:26 
GeneralRe: Returning an Array Pin
Govert van Drimmelen18-Jan-05 0:14
Govert van Drimmelen18-Jan-05 0:14 
GeneralRe: Returning an Array Pin
Anonymous20-Jan-05 20:48
Anonymous20-Jan-05 20:48 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.