vba - 如何从返回对象列表的 C# 方法在 Access VBA 中获取数据
问题描述
我正在通过自定义 DLL调用getInterventions()
在VBA Access 2003应用程序上调用的 C# WebService 方法。该方法的签名如下:
List<Item> getInterventions(string, string, string, string, string, string)
Item是一个自定义的类。
当我尝试检索getInterventions()
VBA 访问代码的结果时,它会弹出 VBA运行时错误 242:需要对象
以下是我的代码:
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)
我已经测试getInterventions()
了通过 Visual Studio 2015 直接从 C# 代码调用,使用与我在 VBA 代码中传递的相同参数并且它有效。这证明这不是参数或方法内容的问题。
我的结论: 我猜这与我不能简单地将 C# 对象列表存储到 VBA 对象中这一事实有关。
任何帮助将不胜感激,谢谢。
解决方案
请自动添加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
对象内容。
推荐阅读
- r - 从表中删除 NULL 值
- python-3.x - 无法使用 python 在 cdk 中为 dynamodb 添加二级索引
- python - 如何使斐波那契数列的递归函数打印出该系列中的任何数字直到极限(参数)
- json - AttributeError:“元组”对象没有属性“转储”
- python - 打印 azure cosmos db 容器
- kubernetes - 如何在 Kubernetes 托管集群中将 --packages 传递给 spark-submit?
- azure-logic-apps - 合并/合并 xml 记录
- javascript - 外部 javascript 不起作用 - 本地文件
- javascript - Javascript 函数是否应该避免通过三元条件拒绝承诺?
- javascript - 我怎样才能修复这个确认密码?