Question

I am trying to create a COM dll. I have compiled the code and registered it with regasm. I have also added the tlb as a reference in a VBA project. However, my VBA project cannot see the methods. Both sets of code are below.

COM Library - Compiled to DLL, registered with regasm as tlb, added tlb to VBA project references

using System;
using System.Net;
using System.Runtime.InteropServices;
using System.Globalization;

namespace ClassLibrary1
{
    [Guid("a81acfd7-ca29-4b71-b45d-d9ffd8930036")]
    public interface ITest
    {
        string HelloWorld(string name);
    }

    [Guid("bb212288-fa1a-432a-9456-e1c3bb78923f"), ComVisible(true)]
    [ClassInterface(ClassInterfaceType.None)]
    public class Test : ITest
    {
        public string HelloWorld(string name)
        {
            return "Hello World! I'm " + name;
        }
    }


}

VBA Project - Simple test. Reference to tlb added. Compile error: Method or Data Member not Found on Call Thing.HelloWorld("Fred")

Sub bob()
    Dim thing As ClassLibrary1.Test
    Call thing.HelloWorld("Fred")
End Sub
Était-ce utile?

La solution

Using com-visible, as user1467261 suggested allowed visibility of the object. The object had to be accessed using CreateObject in the VBA. The code is below.

COM Library - Compiled to DLL, registered with regasm as tlb, added tlb to VBA project references

using System;
using System.Net;
using System.Runtime.InteropServices;
using System.Globalization;

namespace ClassLibrary1
{
    [Guid("a81acfd7-ca29-4b71-b45d-d9ffd8930036")]
    public interface ITest
    {
        string HelloWorld(string name);
    }

    [Guid("bb212288-fa1a-432a-9456-e1c3bb78923f"), ComVisible(true)]
    [ClassInterface(ClassInterfaceType.None)]
    public class Test : ITest
    {
        public string HelloWorld(string name)
        {
            return "Hello World! I'm " + name;
        }
    }


}

VBA

Sub hwn()
    Dim thing As Object
    Dim text As String
    Set thing = CreateObject("ClassLibrary1.Test")
    text = thing.HelloWorld("Fred")
    Debug.Print (text)
End Sub
Licencié sous: CC-BY-SA avec attribution
Non affilié à StackOverflow
scroll top