Click here to Skip to main content
Licence 
First Posted 18 Jul 2004
Views 351,098
Bookmarked 76 times

Create an Automation Add-In for Excel using .NET

By | 3 Aug 2004 | Article
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

About the Author

Govert van Drimmelen

Web Developer

South Africa South Africa

Member



Sign Up to vote   Poor Excellent
Add a reason or comment to your vote: x
Votes of 3 or less require a comment

Comments and Discussions

 
You must Sign In to use this message board. (secure sign-in)
 
Search this forum  
 FAQ
    Noise  Layout  Per page   
  Refresh
GeneralVery nice, but on my machine needed ComVisible Pinmemberm_albert13713:24 2 Aug '10  
GeneralMy vote of 5 Pinmemberm_albert13713:21 2 Aug '10  
Generalsome working examples Pinmembermy interest10:35 22 Jul '10  
GeneralMy vote of 2 Pingroupdhiraj.upadhyay21:02 22 Apr '10  
GeneralUDF Listesd in "Inactive Application Add-Ins" PinmemberJean-Pierre Gervasoni0:32 12 Apr '10  
GeneralRe: UDF Listesd in "Inactive Application Add-Ins" PinmemberMichael Schäuble0:44 11 May '10  
GeneralRe: UDF Listesd in "Inactive Application Add-Ins" PinmemberJean-Pierre Gervasoni3:38 11 May '10  
GeneralC# Method visible in VBA but not reachable PinmemberAsim Mahmood221:32 9 Apr '09  
GeneralThanks and promote more your ExcelDna! PinmemberAlain VIZZINI16:30 9 Mar '09  
Questionhow to make a value in an excel cell read only using c#? Pinmemberabhiram_nayan1:43 22 Jan '09  
Questiona button on the excel active cell? Pinmemberabhiram_nayan2:46 7 Jan '09  
Questionhow to call this " Add2(3,4)" from another c#.2005 programme Pinmembervishal nikam8320:44 16 Dec '08  
AnswerRe: how to call this " Add2(3,4)" from another c#.2005 programme PinmemberGovert van Drimmelen12:08 24 Dec '08  
AnswerRe: how to call this " Add2(3,4)" from another c#.2005 programme Pinmemberjibin.mn1:22 5 Aug '10  
General#REF! Pinmemberabi804:26 11 Aug '08  
GeneralAddin in Excel 2002 getting registered - Add2 function is not shown in function browser in Excel 2002. PinmemberMember 38506453:20 9 Jul '08  
Questioncannot get .dll to work as addin PinmemberKl_Gl23:16 21 Jun '08  
AnswerRe: cannot get .dll to work as addin Pinmembermike198911:28 24 Aug '10  
QuestionUsing dll as an add-in for a different machine Pinmemberwilliamw3:08 7 May '08  
AnswerRe: Using dll as an add-in for a different machine Pinmemberwilliamw6:07 8 May '08  
QuestionIs it possible to create functions that take a range/an array as input? PinmemberJosefLagergren0:48 5 Mar '08  
QuestionInteresting Error Pinmemberpinkfloydfan2:36 26 Oct '07  
GeneralOnly works on my local machine PinmemberAnthonyLondon11:13 25 Oct '07  
It's working fine on my machine with Office 2003 (SP2). I developed it using VS2005. I wanted to give the AddIn to another user for testing. On his machine, .net 2.0 framework and Office 2003 (SP2) are installed (visual studio is not installed on his machine).
 
I copied the .dll file and ran Regasm.
When I open Excel, I can see the Addin in the List of addins (Tools--> Addins), But when i try to enter the function in a cell it's not recognised and on checking in the "Insert Function Dialog box" , my addin is not appearing in the list.
 
I even tried the steps mentioned in this article (kb908002). But still it's not working.
 
Any help to solve this issue is greatly appreciated.
GeneralRe: Only works on my local machine PinmemberAnthonyLondon1:18 26 Oct '07  
QuestionGreat! But how do we add explanation to the functions? [modified] Pinmemberpinkfloydfan23:33 23 Oct '07  

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.

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