Click here to Skip to main content
11,504,892 members (69,678 online)
Click here to Skip to main content

Create an Automation Add-In for Excel using .NET

, 3 Aug 2004 430.4K 79
Rate this:
Please Sign up or sign in to vote.
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:
      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

Share

About the Author

Govert van Drimmelen
Web Developer
South Africa South Africa
No Biography provided

Comments and Discussions

 
GeneralVery nice, but on my machine needed ComVisible Pin
m_albert1372-Aug-10 13:24
memberm_albert1372-Aug-10 13:24 
GeneralMy vote of 5 Pin
m_albert1372-Aug-10 13:21
memberm_albert1372-Aug-10 13:21 
Generalsome working examples Pin
my interest22-Jul-10 10:35
membermy interest22-Jul-10 10:35 
GeneralMy vote of 2 Pin
dhiraj.upadhyay22-Apr-10 21:02
groupdhiraj.upadhyay22-Apr-10 21:02 
GeneralUDF Listesd in "Inactive Application Add-Ins" Pin
Jean-Pierre Gervasoni12-Apr-10 0:32
memberJean-Pierre Gervasoni12-Apr-10 0:32 
GeneralRe: UDF Listesd in "Inactive Application Add-Ins" Pin
Michael Schäuble11-May-10 0:44
memberMichael Schäuble11-May-10 0:44 
Maybe you have your answer already... ? Obviously Excel has trouble to find the path to mscoree.dll, so it helps to register the full path to it. You can add the following lines to the RegisterFunction:
        Dim key As RegistryKey = Registry.ClassesRoot.OpenSubKey("CLSID\\{" + t.GUID.ToString().ToUpper() + 
                   "}\\InprocServer32"), True)
        key.SetValue("", (Environment.SystemDirectory & "\mscoree.dll"), RegistryValueKind.String)
Hope it still helps you. Thumbs Up | :thumbsup:
GeneralRe: UDF Listesd in "Inactive Application Add-Ins" Pin
Jean-Pierre Gervasoni11-May-10 3:38
memberJean-Pierre Gervasoni11-May-10 3:38 
GeneralC# Method visible in VBA but not reachable Pin
Asim Mahmood29-Apr-09 21:32
memberAsim Mahmood29-Apr-09 21:32 
GeneralThanks and promote more your ExcelDna! Pin
Alain VIZZINI9-Mar-09 16:30
memberAlain VIZZINI9-Mar-09 16:30 
Questionhow to make a value in an excel cell read only using c#? Pin
abhiram_nayan22-Jan-09 1:43
memberabhiram_nayan22-Jan-09 1:43 
Questiona button on the excel active cell? Pin
abhiram_nayan7-Jan-09 2:46
memberabhiram_nayan7-Jan-09 2:46 
Questionhow to call this " Add2(3,4)" from another c#.2005 programme Pin
vishal nikam8316-Dec-08 20:44
membervishal nikam8316-Dec-08 20:44 
AnswerRe: how to call this " Add2(3,4)" from another c#.2005 programme Pin
Govert van Drimmelen24-Dec-08 12:08
memberGovert van Drimmelen24-Dec-08 12:08 
AnswerRe: how to call this " Add2(3,4)" from another c#.2005 programme Pin
jibin.mn5-Aug-10 1:22
memberjibin.mn5-Aug-10 1:22 
GeneralAddin in Excel 2002 getting registered - Add2 function is not shown in function browser in Excel 2002. Pin
Member 38506459-Jul-08 3:20
memberMember 38506459-Jul-08 3:20 
Questioncannot get .dll to work as addin Pin
Kl_Gl21-Jun-08 23:16
memberKl_Gl21-Jun-08 23:16 
AnswerRe: cannot get .dll to work as addin Pin
mike198924-Aug-10 11:28
membermike198924-Aug-10 11:28 
QuestionUsing dll as an add-in for a different machine Pin
williamw7-May-08 3:08
memberwilliamw7-May-08 3:08 
AnswerRe: Using dll as an add-in for a different machine Pin
williamw8-May-08 6:07
memberwilliamw8-May-08 6:07 
QuestionIs it possible to create functions that take a range/an array as input? Pin
JosefLagergren5-Mar-08 0:48
memberJosefLagergren5-Mar-08 0:48 
QuestionInteresting Error Pin
pinkfloydfan26-Oct-07 2:36
memberpinkfloydfan26-Oct-07 2:36 
GeneralOnly works on my local machine Pin
AnthonyLondon25-Oct-07 11:13
memberAnthonyLondon25-Oct-07 11:13 
GeneralRe: Only works on my local machine Pin
AnthonyLondon26-Oct-07 1:18
memberAnthonyLondon26-Oct-07 1:18 
QuestionGreat! But how do we add explanation to the functions? [modified] Pin
pinkfloydfan23-Oct-07 23:33
memberpinkfloydfan23-Oct-07 23:33 
GeneralNeed Help Pin
mousum_cp3-Sep-07 2:04
membermousum_cp3-Sep-07 2:04 

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

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

| Advertise | Privacy | Terms of Use | Mobile
Web04 | 2.8.150520.1 | Last Updated 4 Aug 2004
Article Copyright 2004 by Govert van Drimmelen
Everything else Copyright © CodeProject, 1999-2015
Layout: fixed | fluid