Building COM library


I'm trying to build a simple COM component (class Library) usable with from VBA, but so far it isn't working as expected (I recieve a DLL loading error whenever I try to add a reference).

I'm using Visual Studio Community 2017 / Visual BAsic.Net /  .Net Framework 4.6.1, the source code compile, the COM component is Registered, but no way to add a reference to it.
What did I miss ?

For record, here is the source code:
<ComClass(ComClass1.ClassId, ComClass1.InterfaceId, ComClass1.EventsId)> _
Public Class ComClass1

#Region "GUID COM"
    ' Ces GUID fournissent l'identité COM pour cette classe 
    ' et ses interfaces COM. Si vous les modifiez, les clients 
    ' existants ne pourront plus accéder à la classe.
    Public Const ClassId As String = "2f55bb60-0e65-4056-b6cb-d4883b531da6"
    Public Const InterfaceId As String = "5fdb7171-a274-4ee8-94b9-4346050233f5"
    Public Const EventsId As String = "b451bac4-a9b5-4874-bc36-e292dfdf33c5"
#End Region

    ' Une classe COM pouvant être créée doit avoir Public Sub New() 
    ' sans paramètre, sinon, la classe ne sera pas 
    ' inscrite dans le Registre COM et ne pourra pas être créée 
    ' via CreateObject.
    Public Sub New()
    End Sub

    Public Function getValue() As String
        getValue = "bla blah"
    End Function

End Class

Open in new window

LVL 15
Fabrice LambertConsultingAsked:
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

ste5anSenior DeveloperCommented:
1. Did you compile it for the correct target CPU? x86 or x64 depending on your Office (VBA host) platform.
2. Did you register it correctly? regasm path\to\library.dll  [/codebase] /tlb.
3. Did you reference it?

The /tlb switch in step 2 is crucial, cause you need to register the created .tlb file.

The source itself is fine:

Scott McDaniel (Microsoft Access MVP - EE MVE )Infotrakker SoftwareCommented:
You also have to make sure you build it correctly. Here's an article that may be of help:

Essentially you have to make it COM visible, and to register it for COM Interop.
Chinmay PatelChief Technical NinjaCommented:
Hi Fabrice,

What error message you are getting when you try to use it in VBA?

CompTIA Network+

Prepare for the CompTIA Network+ exam by learning how to troubleshoot, configure, and manage both wired and wireless networks.

Fabrice LambertConsultingAuthor Commented:
I had a message saying (if I translate correctly):
"Error while loading the dll".

Weird thing, it now load correctly, but I have no clue why (reboot ? Windows, VS or office update ?).
ste5anSenior DeveloperCommented:
Did you manually register that assembly or did you use the VS check option in the properties? In the second case, it may have registered an older version due to locking by VS. I had this case in the past.
Fabrice LambertConsultingAuthor Commented:
I used VS option.

Only thing changed with my test today is that VS isn't running, and the project is closed.
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Visual Basic.NET

From novice to tech pro — start learning today.