【发布时间】: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<>。您说您正在通过自定义 c# DLL 调用 Web 服务。你不能改变 DLL 来将列表转换为数组吗? -
尝试声明
result As ArrayList。为此,请在“Framework version 3.5”中添加对mscorlib.tlb的引用。其实我会贴一段代码可以自动添加必要的引用...