【问题标题】:Problem returning object from VB.NET COM Assembly into VBA (Access)将对象从 VB.NET COM 程序集返回到 VBA(访问)的问题
【发布时间】:2011-07-16 04:50:28
【问题描述】:

我在 VB .NET 2.0 中有一个程序集,我试图用它来调用 Web 服务。

这将是 COM 可见的,并将结果返回到 VBA 中的 Access。

.NET 程序集通过所有测试并完美执行。

我在从 VBA 调用方法时遇到“对象不支持此属性或方法”错误。

我将其分解为某个正在返回的对象,并向 .NET DLL 添加了一些测试方法。

我想要返回一个“患者”对象。 它看起来像这样(测试起来非常简单):

Option Strict On
Option Explicit On

<ComClass(Patient.ClassId, Patient.InterfaceId, Patient.EventsId)> _
Public Class Patient

#Region "COM GUIDs"
    ' These  GUIDs provide the COM identity for this class 
    ' and its COM interfaces. If you change them, existing 
    ' clients will no longer be able to access the class.
    Public Const ClassId As String = "672dfbd9-8f3a-4ba2-a33d-89fef868f2b9"
    Public Const InterfaceId As String = "74a9c54c-4427-4d31-8220-3258ecda345d"
    Public Const EventsId As String = "dc25515e-1bb7-4a66-97d5-270c00d792a9"
#End Region

    Public Sub New()

        MyBase.New()

    End Sub

    Public Property StorePatientID() As Integer
        Get
            Return m_StorePatientID
        End Get
        Set(ByVal value As Integer)
            m_StorePatientID = value
        End Set
    End Property
    Private m_StorePatientID As Integer
End Class

一个对象真的可以这么简单。

我有一个方法只返回一个虚拟记录,只是为了测试它:

Public Function GetPatientTest() As Patient
        Dim patient As New Patient

        patient.StorePatientID = 99

        Return patient
    End Function

由于上述错误而失败。

然而,

这个方法成功了!

Public Function GetPatientArrayTest() As Patient()

        Dim strings As New List(Of Patient)
        Dim patient As New Patient

        patient.StorePatientID = 99

        strings.Add(patient)

        Return strings.ToArray

    End Function

DLL 通过“属性”页面可见。 构建到项目/bin/debug,总是进行重建。 当我在 VBA 中查看它时,似乎总是使用新方法等进行更新,所以不要认为它正在查看旧版本。

这些方法显然没有有趣的依赖关系。

真的很挣扎。

编辑:

2011 年 3 月 16 日更新 - 添加了 VBA 脚本

Public Function FindPatientsTest(ByVal surname As String, ByVal surnameBeginsWith As Boolean, ByVal forename As String, ByVal forenameBeginsWith As Boolean, ByVal dateOfBirth As String)


Dim token As String
token = Login()

Dim patient As SCIStoreWS60.patient
Set patient = New SCIStoreWS60.patient

'// This doesn't work.
'// When adding a "Watch" to the function, I can see it returns an "Object/Patient" and is the correct results
'// When adding a "Watch" to the variable "patient" I can see it is a "Patient/Patient"
patient = sciStore.GetPatientTest()

'// This works fine
Dim something As Variant
something = sciStore.GetPatientArrayTest()

End Function

5 分钟后更新 2011 年 3 月 16 日 - 责备自己

对不起,我刚刚解决了。

我需要“设置”患者变量。

Set patient = sciStore.GetPatientTest()

为什么我不需要为“某事”变体执行此操作?

【问题讨论】:

  • 也发布失败的 vba 代码。
  • 您能否在您的 VBA 代码确认中包含您的“成功”数组返回实际上包含一个 ID 为 99 的 Patient,可从 Access 访问?
  • 当然,添加了 VBA 脚本。我可以通过“手表”看到该函数实际上正在返回一个“对象/患者”,并且它返回的患者是正确的。所以看起来好像函数被正确调用了。看起来 VBA 在将结果分配给我声明的变量时遇到问题?
  • 刚刚解决了。需要“设置”患者变量。奥卡姆剃刀。当您开始判断 COM/Visual Basic 的基本工作原理时,您可能做了一个愚蠢的选择。

标签: vb.net vba com


【解决方案1】:

所以,是的,您需要Set 对象引用,而不是数组。

【讨论】:

    猜你喜欢
    • 2017-09-10
    • 2011-06-07
    • 2013-08-15
    • 2011-05-19
    • 2017-05-16
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多