exceldllcomtlb

.NET Core / .NET 6: Creating a TLB or DLL that can be added as reference in VBA


I am trying to do basically what it says in the title: I have created a class library (dll) using .NET 6.0 and I would like to add that as a reference in an Excel/Access VBA document. I diligently followed the steps here:

https://learn.microsoft.com/en-us/dotnet/core/native-interop/expose-components-to-com

No matter what I try, I cannot add the resulting dll as a reference in Excel. I just keep getting the message: 'Can't add reference to the specified file'.

A bit more info: I am able to create the Assembly.comhost.dll file, and I think I have done everything correctly, but no joy.

This is rather frustrating, as doing this in .NET Framework is but a click of a couple of checkboxes. In .NET Core (.NET 6) It seems to be a bit of a nightmare. Also, being able to create a tlb from the dll was always a perfect confirmation that you would be able to add your reference in Excel.

ANY help is most welcome!

Thanks!


Solution

  • in the tutorial you hav ementioned, ther is a topic "Register the COM host for COM". here a file named "ProjectName.comhost.dll" is used to register the assembly to the registry as a COM type library. After doing this, I could use the assembly in VBS with the following code:

    set projectObject = CreateObject("ProjectNamespace.ProjectClass")
    newValue = projectObject.ComputeNewValue("abc")
    

    Right now, I am not able to establish a reference to that type library in my Excel 2016, but the code sample works in VBA as well.