How to enable early binding of VBA object variable

2019-05-28 06:39发布

I seem to be having trouble getting my COM class library (.NET-based) to support early binding.

I am creating a class library in VB.NET 2003 for use in Office 2003 VBA (and later for Office 2010). Documentation @ StackOverflow and elsewhere has brought me to this bit of code:

    Imports System.Runtime.InteropServices

    <InterfaceType(ComInterfaceType.InterfaceIsDual), _
    ComVisible(True), _
    Guid("<some valid GUID>")> _
    Public Interface _TestCOMClass
        Function Test() As String
    End Interface

    <ClassInterface(ClassInterfaceType.None), _
    ComVisible(True), _
    Guid("<another valid GUID>"), _
    ProgId("TestCOMDLL.TestCOMClass")> _
    Public Class TestCOMClass
        Implements _TestCOMClass
        Public Function Test() As String Implements _TestCOMClass.Test
            Return "Test value"
       End Function
    End Class

The solution is set to compile with COM Interop. It builds succesfully and the ProgID then appears in the References list in VBA.

I use this in VBA by setting the appropriate reference and then declaring variables of the appropriate type and instantiating my class. Here is where it gets mysterious.

    Dim EarlyBird As TestCOMDLL.TestCOMClass
    Dim LateBird  As Object

    Set EarlyBird = New TestCOMDLL.TestCOMClass
    ' This is my preferred instantiation method, but results in a 
    ' "Does not support Automation or expected interface" error

    Set EarlyBird = CreateObject("TestCOMDLL.TestCOMClass")
    ' This is my 2nd best instantiation method, 
    ' but results in a Type Mismatch error

    Set LateBird = CreateObject("TestCOMDLL.TestCOMClass")
    MsgBox LateBird.Test
    ' This works, but has all the disadvantages of late binding

So I can reference my library, declare object variables of the appropriate type, and instantiate my class, but I cannot assign the instantiated object reference to my typed variable, only to a variable of type Object. Also, instantiating the New keyword appears to be supported (Intellisense offers the library and class as options), but fails at runtime.

What is lacking in my VB.NET code or build settings that keeps early binding from working?

PS: Another way of putting my problem is that I've tried the solution in this StackOverflow thread and found that what AnthonyWJones says

You can also reference the dll and use early binding:

is not true in my case... :-(

0条回答
登录 后发表回答