Making an MS Excel User-Defined-Functions - c#

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.

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

Related

How can I fix Run-time error 430 from importing custom DLL in VBA project

I've struggle several hours on that and I can't find what I'm doing wrong.
I created a new C# dll project, here is the content of the only class it contain:
using System;
using System.Runtime.InteropServices;
namespace PolygonSl {
[ComVisible(true)]
[ClassInterface(ClassInterfaceType.AutoDual)]
public class Config {
[ComVisible(true)]
public string GetCompany() {
return "POL";
}
}
}
I basically remove everything from it trying to make it work, the only reference is System.
I checked the Make assembly COM-Visible flag on the Assembly Information and my project is signed (seams required for codebase).
It compiling fine, after that, I called RegAsm.exe, giving it my dll, I added /codebase and /tlb, the command is successful.
When I go to my VBA project, I can add my new tlb file to the references, working fine. After, I can use it in my code, the autocomplete is working and I can compile with no errors.
Then, when I execute, I got this:
Run-time error '430':
Class does not support Automation or does not support expected interface
Here is my code sample in the VBA:
Private Sub Button1_Click()
'With CreateObject("PolygonSl.Config")
With New PolygonSl.Config
MessBox .GetCompany, MB_OK, "Test"
End With
End Sub
I tried late binding and my code is running fine with it but I'd like to be able to use the autocomplete.
Anyone have a suggestion on what I could try to make it work?
Edit (Adding some details on my environment)
I work on VS2008 for projects related to Dynamics SL (one of the Microsoft ERPs)
I'm on Windows Server 2008 R8 Standard, running from VMWare
Compiling on Framework 3.5, Release, x86, Dynamics SL client is 32 bits
I tried my dll on Dynamics but also on Excel to be sure that the problem was not Dynamics ;)
I think you need to define an interface to be able to see getcompany.
using System;
using System.Runtime.InteropServices;
namespace PolygonSl
{
[Guid("6DC1808F-81BA-4DE0-9F7C-42EA11621B7E")]
[System.Runtime.InteropServices.ComVisible(true)]
[System.Runtime.InteropServices.InterfaceType(ComInterfaceType.InterfaceIsDual)]
public interface IConfig
{
string GetCompany();
}
[Guid("434C844C-9FA2-4EC6-AB75-45D3013D75BE")]
[System.Runtime.InteropServices.ComVisible(true)]
[System.Runtime.InteropServices.ClassInterface(ClassInterfaceType.None)]
public class Config : IConfig
{
public string GetCompany()
{
return "POL";
}
}
}
You can generate the interface automatically by placing the cursor in the class definition and using Edit.Refactor.ExtractInterface.
I'd have to admit that I'm at the absolute edge of my abilities here and the above is put together based on examples I've seen elsewhere.
Edit
The following test code works fine on my PC
Option Explicit
Sub polygontest()
Dim my_polygon As SOPolygon.Config
Set my_polygon = New SOPolygon.Config
Debug.Print my_polygon.GetCompany
End Sub
Where SOPolygon is the project name.

LaunchAdvancedAssociationUI in C# -> Element not found on Windows 8

I'm trying to set up a way to manage file associations for my program in C#. I already set the correct values in the registry with WiX, and found a wrapper for ApplicationAssociationRegistrationUI which should allow me to open the GUI to set file associations. But it doesn't work. I get the following exception: Element not found. (Exception from HRESULT: 0x80070490)
The wrapper:
namespace FileAssociation
{
[ClassInterface(ClassInterfaceType.None)]
[ComImport]
[Guid("1968106d-f3b5-44cf-890e-116fcb9ecef1")]
[TypeLibType(TypeLibTypeFlags.FCanCreate)]
public sealed class ApplicationAssociationRegistrationUI : IApplicationAssociationRegistrationUI
{
[MethodImpl(MethodImplOptions.InternalCall)]
public extern void LaunchAdvancedAssociationUI(string appRegistryName);
}
[CoClass(typeof(ApplicationAssociationRegistrationUI))]
[ComImport]
[Guid("1f76a169-f994-40ac-8fc8-0959e8874710")]
[InterfaceType(ComInterfaceType.InterfaceIsIUnknown)]
[TypeLibImportClass(typeof(ApplicationAssociationRegistrationUI))]
public interface IApplicationAssociationRegistrationUI
{
void LaunchAdvancedAssociationUI([MarshalAs(UnmanagedType.LPWStr)] string appRegistryName);
}
}
Usage:
var assocUi = new ApplicationAssociationRegistrationUI();
try
{
assocUi.LaunchAdvancedAssociationUI(InstanceManager.ProgId);
}
catch
{
MessageBox.Show("Could not display the file association manager. Please repair the installation and try again.", "Error", MessageBoxButton.OK, MessageBoxImage.Warning);
}
finally
{
Marshal.ReleaseComObject(assocUi);
}
Again, all the correct keys exist in the registry. This is not the first time COM Interop fails miserably for me so I am beginning to think that I must be missing something important. I tried checking "Register for COM Interop" in the project properties, and I tried making it COM-Visible.
I am aware that this only works on Vista or newer, which is fine since my program doesn't support XP anyway. I'm testing it on Windows 8.1, both as Admin and as normal user.
EDIT: It works on Windows 7! On MSDN it does not say that this API was deprecated in Win8...
What have I done wrong? Is there an easier way to do this which I don't know about?
Finally found the problem!!!
Starting from Windows 8, the program needs to have company information (I think this is a bug, since it isn't mentioned on Microsoft's site.)
So make sure to fill out this attribute in AssemblyInfo.cs:
[assembly: AssemblyCompany("YourCompany")]
If it's an empty string it won't work!

C# external library (Lua) call problem

I'm new to programming in C# (VS2010) .Net (4.0) and I'm encountering I couldn't solve by myself since some days already.
I'm using an external scripting language (Lua) in my C# code.
To do so I use LuaInterpreter built for .Net 4.0
First try:
The project is a console application -> the program works fine when I try to call a Lua class.
Second try:
The project is a class Librrary COM used from Excel -> The class library compile fine and my user defined functions work fine within Excel. But when I try to call a Lua class it crashed saying that the Lua assembly is missing.
Could not load file or assembly 'lua51, Version=0.0.0.0, Culture=neutral, PublicKeyToken=1e1fb15b02227b8a' or one of its dependencies. Strong name validation failed. (Exception from HRESULT: 0x8013141A)
To reproduce the problem :
1- You need to get LuaInterface .Net 4.0 from
http://www.mdome.org/2011/05/16/luainterface-for-csharp-net-4-custom-build/
2- Add LuaInterface as a reference in your project
3- Copy the Lua51 DLL in the building directory (I put my Excel sheet there too)
4- Copy the code for the Class Library
using System;
using System.Collections.Generic;
using System.Runtime.InteropServices;
using Microsoft.Win32;
using Excel = Microsoft.Office.Interop.Excel;
using LuaInterface;
namespace POC
{
[ClassInterface(ClassInterfaceType.AutoDual)]
[ComVisible(true)]
public class Functions
{
public int test()
{
Lua lua = new Lua();
return 0;
}
#region Class in Excel
[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();
}
#endregion
}
}
The function that crashed is the test function when called from Excel
I would take any help on that
Thanks
SInce it appears to be signed, try to put Lua51 into the GAC and see if it works. Probably you can try even by putting Lua15.dll in the same path of excel.exe.
I've had lots of issues with .NET, LuaInterface, and Lua5.1 interracting on 64-bit machines. Lua5.1 only compiles 32-bit and this requires you to (I believe) build the LuaInterface project as 32-bit as well. Try changing "Project -> Properties -> Build -> Platform Target" to "x86" in your .NET projects.

ScriptSharp ClockLabel example with 0.6.2

I'm developing in Visual Studio 2010 and I've just downloaded and installed Script# 0.6.2 for VS 2010. I'm trying to follow the clock example in the Read Me pdf but can't get it to compile.
I've created a new Script# Class Library project inside my solution called Clock, renamed the .cs file to ClockBehaviour and added the following code as per the example:
using System;
using System.DHTML;
using ScriptFX;
using ScriptFX.UI;
namespace Clock {
public class ClockBehavior : Behavior {
private int _intervalCookie;
public ClockBehavior(DOMElement domElement, string id) : base(domElement, id) {
_intervalCookie = Window.SetInterval(OnTimer, 1000);
}
public override void Dispose() {
if (_intervalCookie != 0) {
Window.ClearInterval(_intervalCookie);
} base.Dispose();
} private void OnTimer() { DateTime dateTime = new DateTime(); DOMElement.InnerHTML = dateTime.Format("T"); }
}
}
When I try and compile the project I get errors saying that the System.DHMTL, ScriptFX and ScriptFX.UI namespaces could not be found (and some others, but I guess by fixing these errors the others will fall out).
It feels like I'm not referencing the correct projects/dlls. In the References for the project I have mscorlib and Script.Web. I've tried using the object browser find the classes (such as Behavior) in other namespaces but with no luck. I've added all of the .dlls from the ScriptSharp folder in Program Files but the namespaces still can't be found.
Any help would be very much appreciated,
Thanks,
Hugh
the sample docs are a bit out of date - look at the phot sample in the samples download : http://projects.nikhilk.net/Content/Projects/ScriptSharp/Sample.zip
See http://projects.nikhilk.net/ScriptSharp/Conceptual-What
You need to reference ssfx.Core.dll which should be installed with Script#
(Alternatively, see pp 23-24 of the pdf you linked...)

Calling C# dll in vbscript

I am trying to call a C# dll from QTP (uses vbscript). I have tried a number of things with no success:
Visual Studio 2010
Create C# class libary (st.dll)
code:
using System;
using System.Collections.Generic;
using System.Text;
namespace st
{
public class Class1
{
public static int GetValue()
{
return 34;
}
}
}
regasm /codebase st.dll
fails 'because it is not a valid .NET assembly'
In QTP/vbscript, I have tried
extern.Declare micInteger, "GetValue", "e:\st.dll", "GetValue"
Returns message: 'Invalid procedure call or argument'
Regardless of QTP, I would greatly appreciate any insight on how to call the c# dll from a .vbs file.
I was able to get this working by doing the following:
Create a new C# dll in VS 2010.
namespace st4
{
public class st4_functions
{
public int GetValue()
{
return 34;
}
}
}
In QTP I added the following lines:
Set obj = DotNetFactory.CreateInstance("st4.st4_functions", "c:\\st4.dll")
MsgBox obj.GetValue()
Thanks to all that responded to my problem. Though I did not do the COM solution, it got me thinking that I could stay with .NET and led to this solution. Good job all!
EDIT:
I created a blog post to detail the steps and provide additional information:
http://www.solutionmaniacs.com/blog/2012/5/29/qtp-calling-c-dll-in-vbscript.html
As Marc said, but I think it merits an answer. If you ensure that your dll will be available though the COM mechanics, your script should be able to call into it with things like CreateObject.
How to register .NET assembly for COM interop
Your function is static. Static class members can't be matched up to interface members, and if it can't implement a .NET interface then it certainly won't implement a COM interface.

Categories

Resources