简体   繁体   中英

Making an MS Excel User-Defined-Functions

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.

PS 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.

你可以试试这个库https://exceldna.codeplex.com ,它简化了很多UDF的创建。

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM