Click here to Skip to main content
15,896,118 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi,

I've been trying to implement a simple function addNumbers(double a, double b) in C# and call it from VBA. I've proceeded as here: Extend your VBA code with C#, VB.NET, or C++/CLI[^]

Code in C#:

C#
using System
using System.Runtime.InteropServices; // Guid, ClassInterface, ClassInterfaceType
using System.Globalization; // CultureInfo
[Guid("97E1D9DB-8478-4E56-9D6D-26D8EF13B100")]
public interface IAddNumbers {
    double addNumbers(double a, double b)
};

[Guid("BBF87E31-77E2-46B6-8093-1689A144BFC6")]
[ClassInterface(ClassInterfaceType.None)]
public class TestClass : IAddNumbers {
    public double addNumbers(double a, double b) {
        return a + b;
    {
}

VBA:
first I registered the file TestClass.dll in the registry and then added a reference in the VBA environment as indicated in the website above. The problem is that VBA does not see TestClass at all. I cannot declare a variable like this:

VB
Dim addNumObj as TestClass
or
Dim addNumObj as new TestClass


Does anyone know why this doesn't work/VBA is not seeing the .dll?
What are the Guids for and how are they generated? I took the Guids from the example on the website.

Many thanks for any help.
Posted
Updated 31-Aug-15 20:46pm
v2

1 solution

Seems, your .dll is ready to expose COM, but you didn't mention that project's settings have been properly set. Please, read this: Generics and Com Visible .NET libraries[^]

For further information, please see:
Example COM Class (C# Programming Guide)[^]
Exposing .NET Components to COM[^]

If you want to use this .dll on non-development machine, you have to register it in GAC using Regasm tool[^].
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900