2

COM クラス ライブラリ (.NET ベース) で早期バインディングをサポートするのに問題があるようです。

Office 2003 VBA (および後で Office 2010) で使用するために、VB.NET 2003 でクラス ライブラリを作成しています。ドキュメンテーション @ StackOverflow および他の場所で、このコードにたどり着きました。

    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

ソリューションは、COM Interop でコンパイルするように設定されています。正常にビルドされ、ProgID が VBA の参照リストに表示されます。

これを VBA で使用するには、適切な参照を設定してから、適切な型の変数を宣言し、クラスをインスタンス化します。ここが神秘的なところです。

    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

そのため、ライブラリを参照し、適切な型のオブジェクト変数を宣言し、クラスをインスタンス化できますが、インスタンス化されたオブジェクト参照を型付き変数に割り当てることはできず、Object 型の変数にのみ割り当てることができます。また、New キーワードのインスタンス化はサポートされているように見えますが (Intellisense はライブラリとクラスをオプションとして提供しています)、実行時に失敗します。

VB.NET コードまたはビルド設定に欠けているものは何ですか? 事前バインディングが機能しません。

PS: 私の問題を別の言い方をすれば、この StackOverflow スレッドで解決策を試してみたところ、AnthonyWJones が言っていることがわかりました。

dll を参照して、事前バインディングを使用することもできます。

私の場合は真実ではありません... :-(

4

0 に答える 0