I am trying to create a custom function for MS Excel in C #.
But no matter what I try, when I try to add an add-in to Excel, I always get the infamous "The selected file does not contain a new automation server or you do not have sufficient privileges to register an automation server error.
Here is the code I took and an online example to try:
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
Solutions. I found and tried without success:
- [ClassInterface (ClassInterfaceType.AutoDual)], as shown in the code
- [ComRegisterFunctionAttribute] and [ComUnregisterFunctionAttribute], as shown in the code
- regasm / codebase did nothing
- / " COM-" (VS )
- [: ComVisible (true)] true
- stackoverflow: COM- Excel, VB.NET, ?
- - .
- Ran Excel
, , , , , , , , , , ! !
, .
P.S. , - . - , , , .