0
votes

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
1
call thing.HelloWorld('Fred') - Tony Hopkinson
@TonyHopkinson That isn't the issue, but the code did need to have that included. I've added it above. The issue is with the line including the ClassLibrary1.Test reference. The class isn't recognized. - steventnorris
I just repeated all your steps and in worked OK. After adding the reference in VBA, are you able to see the interface in VBA Object Browser (F2) ? - AFischbein
Also, did you check the "Make assembly COM-Visible" box in Project Application properties? - AFischbein
@user1467261 I did not check "Make assembly COM-Visible". Where is that located at? - steventnorris

1 Answers

1
votes

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