4

i'm trying to create a User Defined Function for MS Excel in C#.

But no matter what I try, when I try to add the Add-in to Excel I always get the infamous "The file you have selected does not contain a new automation server, or you do not have sufficient privileges to register the automation server" error.

Here's the code that I took from and online example just to try it out:

// C#

using System;
using System.Runtime.InteropServices;
using Microsoft.Win32;

namespace AutomationAddin
{
    [ClassInterface(ClassInterfaceType.AutoDual)]
    public class MyUdf
    {
        public MyUdf()
        {
        }

        public double addMeTest(double x, double y)
        {
            return x + y;
        }

        [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");
        }
    }
}

I tried this with MS Visual Studio 2012 on Excel 2013 x64 and Excel 2010 x86

SolutionsI've found and tried with no success:

  • [ClassInterface(ClassInterfaceType.AutoDual)] as seen in the code
  • [ComRegisterFunctionAttribute] AND [ComUnregisterFunctionAttribute] as seen in the code
  • regasm /codebase did nothing as well
  • Turning on/off "Register COM interop" (VS running as admin when building)
  • [assembly: ComVisible(true)] set to true
  • Tried different code examples from the web
  • Read this on stackoverflow: How to get COM Server for Excel written in VB.NET installed and registered in Automation Servers list?
  • I've also tried all of the above together - no luck here
  • Ran Excel in admin mode

So please guys, if you can tell me what am I missing here and maybe even tell me what should I do to make it work I would be so grateful! Thanks in advance!

I will gladly provide any additional info if needed.

P.S. Haven't had any sleep for two nights now so I might be screwing something up in a really stupid way. If someone could test this code if it works and tell me their project setup it just might help.

Community
  • 1
  • 1
Janis Vepris
  • 587
  • 3
  • 13

1 Answers1

2

You can try this library https://exceldna.codeplex.com, it simplifies creation of UDFs a lot.

user626528
  • 13,999
  • 30
  • 78
  • 146
  • Thank you very much for your suggestion. I've successfully created XLL UDF's before with this tool, it's great. But the problem is that I have a university assignment and they want me to create a COM automation Add-in, that's a requirement :( – Janis Vepris Dec 20 '13 at 03:07