如何启用 VBA 对象变量到 .NET 中创建的 COM 类的早期绑定
我似乎无法让我的 COM 类库(基于 .NET)支持早期绑定。
我正在 VB.NET 2003 中创建一个类库,以便在 Office 2003 VBA(以及更高版本的 Office 2010)中使用。 @ 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
因此,我可以引用我的库,声明适当类型的对象变量,并实例化我的类,但我无法将实例化的对象引用分配给我的类型变量,只能分配给对象类型的变量。此外,似乎支持实例化 New 关键字(Intellisense 提供库和类作为选项),但在运行时失败。
我的 VB.NET 代码或构建设置中缺少什么,导致早期绑定无法正常工作?
PS:解决我的问题的另一种方式是我尝试了 这个 StackOverflow 线程 发现 AnthonyWJones 所说的
您还可以引用 dll 并使用早期绑定:
在我的情况下不是这样...:-(
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... :-(
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论