UDF in Excel.

EDN Admin

Well-known member
Joined
Aug 7, 2010
Messages
12,794
Location
In the Machine
Hi!
Id like to access to some udf, written in a class library, from excel.
Normally, when I use .net framwork 4 and I check "Register for COM Interop" there is no problem, but It should be able to register the .dll manually or create somehow an installer for this purpose.
There is a constraint, the .dll should be compiled in v3.5 or lower. So, I create a class library project in visual studio, I add "everything" needed:

<span style="font-size:small
<pre class="prettyprint" style=" [Guid("BA8CEA11-D343-4A76-B5B8-43AB78A3E992")]
[ClassInterface(ClassInterfaceType.AutoDual)]
[ComVisible(true)]
[UdfClass][/code]
<br/>
Also [UdfMethod] when needed and

<pre class="prettyprint" style=" [ComRegisterFunctionAttribute]
public static void RegisterFunction(Type type)
{
Registry.ClassesRoot.CreateSubKey(GetSubKeyName(type, "Programmable"));
RegistryKey key = Registry.ClassesRoot.OpenSubKey(GetSubKeyName(type, "InprocServer32"), true);
key.SetValue("", System.Environment.SystemDirectory + @"mscoree.dll", RegistryValueKind.String);
}

[ComUnregisterFunctionAttribute]
public static void UnregisterFunction(Type type)
{
Registry.ClassesRoot.DeleteSubKey(GetSubKeyName(type, "Programmable"), false);
}

private static string GetSubKeyName(Type type, string subKeyName)
{
System.Text.StringBuilder s = new System.Text.StringBuilder();
s.Append(@"CLSID{");
s.Append(type.GUID.ToString().ToUpper());
s.Append(@"}");
s.Append(subKeyName);

return s.ToString();
}[/code]

Then, I compile the .dll with the .net framework v3.5 (I also tryied the version v2) and I register the .dll like this:
<pre class="prettyprint" style=" C:WindowsMicrosoft.NETFrameworkv2.0.50727>RegAsm.exe <path>mydll.dll[/code]
From Excel I add the the add in from the automation list. So far its ok, but now when I try to use the methods, I dont have any new methods in the category combobox.
Any idea?
Thank you in advance,
Roberto.







<br/>
<br/>

View the full article
 
Back
Top