Vb.net 如何启用VBA对象变量到.NET中创建的COM类的早期绑定
我似乎很难让基于.NET的COM类库支持早期绑定 我正在VB.NET2003中创建一个类库,用于Office2003VBA和更高版本的Office2010。StackOverflow和其他地方的文档让我想到了以下代码:Vb.net 如何启用VBA对象变量到.NET中创建的COM类的早期绑定,vb.net,vba,com,createobject,early-binding,Vb.net,Vba,Com,Createobject,Early Binding,我似乎很难让基于.NET的COM类库支持早期绑定 我正在VB.NET2003中创建一个类库,用于Office2003VBA和更高版本的Office2010。StackOverflow和其他地方的文档让我想到了以下代码: Imports System.Runtime.InteropServices <InterfaceType(ComInterfaceType.InterfaceIsDual), _ ComVisible(True), _ Guid("<
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
因此,我可以引用我的库,声明适当类型的对象变量,并实例化我的类,但我不能将实例化的对象引用分配给我的类型化变量,而只能分配给object类型的变量。此外,Intellisense似乎支持实例化新关键字,将库和类作为选项提供,但在运行时失败
我的VB.NET代码或构建设置中缺少什么使早期绑定无法工作
PS:另一种解决问题的方法是,我尝试了解决方案,发现AnthonyWJones说的是
您还可以引用dll并使用早期绑定:
在我的情况下不是这样的…:- 这只会在以后添加GUID并忘记重新注册程序集、创建类型库或更新VBA中的类型库引用时出错。使用SysInternals的ProcMon查看它在注册表中的位置。请查看类似的问题:只有当您稍后添加GUID并忘记重新注册程序集、创建类型库或更新VBA中的类型库引用时,可能会出现重复的问题。使用SysInternals的ProcMon查看它在注册表中的位置。查看类似的问题:可能重复
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