Building COM library

Fabrice Lambert
Fabrice Lambert used Ask the Experts™

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

Watch Question

Do more with

Expert Office
EXPERT OFFICE® is a registered trademark of EXPERTS EXCHANGE®
ste5anSenior Developer

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:

Most Valuable Expert 2012
Top Expert 2014

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 Technology Ninja
Distinguished Expert 2018

Hi Fabrice,

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

Bootstrap 4: Exploring New Features

Learn how to use and navigate the new features included in Bootstrap 4, the most popular HTML, CSS, and JavaScript framework for developing responsive, mobile-first websites.

Fabrice LambertConsulting
Distinguished Expert 2017


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 Developer

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 LambertConsulting
Distinguished Expert 2017


I used VS option.

Only thing changed with my test today is that VS isn't running, and the project is closed.

Do more with

Expert Office
Submit tech questions to Ask the Experts™ at any time to receive solutions, advice, and new ideas from leading industry professionals.

Start 7-Day Free Trial