vbavb.netcomcom-interopcom-interface

Not able to use my simple VB class library in VBA


So this is my first post in this great community and I'm an absolute beginner, I'm sure I'll get good advice from here.

I mad this simple VB class library in Visual Studio 2017

Public Class Addition
    Public Function Add(No1 As Long, No2 As Long) As Long
        Return No1 + No2
    End Function
End Class

I made it COM visible and checked Register for COM interop. and build the project.

In my access VBA project, I've added the reference to my Dll without a problem and put the following code behind the click event of a button.

Private Sub Command0_Click()
Dim myCalc As ShwDolphin.Addition
Set myCalc = New ShwDolphin.Addition
Dim Num As Long
Num = myCalc.Add(2, 5)
Debug.Print Num
End Sub

"ShwDolphin" is VB assembly name.

But I always get this error message "Runtime error 429 Active X component can't create an object"

Can you please tell me what I'm doing wrong here? This is driving me crazy.

Thank you very much.


Solution

  • Reading the comments, my guess is that you have a difference in what I call "bitness". Default projects in Visual Studio are generally 32-bits. If you build a 32-bit COM DLL, it can only be loaded by 32-bit processes. If your Office installation is 64-bit, it will never work. What you will need to do is build/register it for 64-bits.

    Now, if you build for MSIL and not any specific processor (x86 or x64), you don't have to really rebuild for 64-bits. All that is necessary is to register for 64-bits. (The default build configuration is MSIL.)

    So you need to use the 64-bit regasm in C:\windows\Microsoft.NET\Framework64\v4.0.30319\regasm.exe

    use that regasm with options of /tlb /codebase "thenameofyour.dll"

    If you built x64 and Office is 32-bit, then do the opposite: use the 32-bit regasm, but odds are that you are using defaults which is 32-bit.

    You can also look at this answer:activex can't create object by vbs but can by classic asp