【问题标题】:How to get data in Access VBA from a C# method that returns an object List如何从返回对象列表的 C# 方法在 Access VBA 中获取数据
【发布时间】:2021-10-13 00:53:30
【问题描述】:

我正在通过自定义 DLL 在 VBA Access 2003 应用程序上调用名为 getInterventions() 的 C# WebService 方法。该方法的签名如下:

List<Item> getInterventions(string, string, string, string, string, string)

Item 是自定义的类。

当我尝试在 VBA 访问代码上检索 getInterventions() 的结果时,它会弹出 VBA 运行时错误 242 : object required

以下是我的代码:

        Dim WsObject As Object
        Dim result As Object

        Set WsObject = CreateObject("Namespace1.Path.To.Class") 'This isn't the actual DLL path as I cannot share that
        
        'Set result = WsObject .getSingleIntervention("123, "0", "123456789", "") ' this works
        
         Set result = WsObject .getInterventions("", "", "123456789", "", "", "") 'this doesn't work
        
         If result Is Nothing Then
           'do some stuff
        Else
           'do some other stuff
        End If

getSingleIntervention() 是一个类似的方法,它返回单个对象而不是对象列表。返回此方法的结果没有问题(请参阅注释行)。这证明 WS 和 DLL 调用都有效。该方法定义如下:

Item getSingleIntervention(string, string, string, string)

我已经测试了通过 Visual Studio 2015 直接从 C# 代码调用 getInterventions(),使用与我在 VBA 代码中传递的相同参数,并且它有效。这证明不是参数或方法内容的问题。

我的结论: 我猜这与我不能简单地将 C# 对象列表存储到 VBA 对象中这一事实有关。

任何帮助将不胜感激,谢谢。

【问题讨论】:

  • VBA 不是 VB.Net。它不支持.Net,因此无法处理List&lt;&gt;。您说您正在通过自定义 c# DLL 调用 Web 服务。你不能改变 DLL 来将列表转换为数组吗?
  • 尝试声明result As ArrayList。为此,请在“Framework version 3.5”中添加对mscorlib.tlb 的引用。其实我会贴一段代码可以自动添加必要的引用...

标签: vba ms-access


【解决方案1】:

请自动添加mscorlib.tlb引用,运行下一段代码:

Sub addMscorlibRef() 'necessary to use ArrayList
  'Add a reference to 'Mscorlib.dll':
  'In case of error ('Programmatic access to Visual Basic Project not trusted'):
  'Options->Trust Center->Trust Center Settings->Macro Settings->Developer Macro Settings->
  '         check "Trust access to the VBA project object model"
  If dir("C:\Windows\Microsoft.NET\Framework64\v4.0.30319", vbDirectory) = "" Then _
     MsgBox "You need to install ""Framework version 3.5"".": Exit Sub
   On Error Resume Next
   Application.VBE.ActiveVBProject.References.AddFromFile "C:\Windows\Microsoft.NET\Framework64\v4.0.30319\mscorlib.tlb"
   If err.Number = 32813 Then
        err.Clear: On Error GoTo 0
        MsgBox "The reference already exists...": Exit Sub
  Else
        On Error GoTo 0
        MsgBox """Mscorlib"" reference added successfully..."
  End If
End Sub

然后尝试声明Dim result As ArrayList

ArrayList 与 C# 中使用的相同。然后,调整 dll 以使用这样的对象。正如您所推断的,任何能够在 VBA 中使用的对象都不能接收 C#list 对象内容。

【讨论】:

  • 应该改用这个吗? Dim result ----- Set result = CreateObject("System.Collections.ArrayList") ----- result = MyObject.getInterventions("", "", "123456789", "", "", "")
  • 另外,这样做的重点是避免必须编辑 DLL,因为这对我们的客户来说不是很方便。不过,我不确定这是否可能。
  • @如果你不调整 dll,它不会有太大的不同。它是早期绑定。我建议了参考,只是为了获得智能感知建议。这个想法是没有 VBA 对象能够处理 C# list 对象结果。至少,我不知道这样一个。如果现有的dll不起作用,我担心客户会不太满意。您将提供更新,更改以前的 dll。但是,您在将它交付给客户之前没有检查它吗?
  • 这不是检查的初始规范的一部分。客户决定在测试阶段改变主意,现在我们正试图找到一种方法来实现它。不幸的是,这样做的唯一方法似乎是在 WebService 中为此目的添加一个新方法,这也需要更新 DLL。不方便,但就是这样。非常感谢您的帮助。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2016-01-21
  • 1970-01-01
  • 1970-01-01
  • 2020-05-12
相关资源
最近更新 更多