我正在通過自定義 DLL呼叫getInterventions()在VBA Access 2003應用程式上呼叫的 C# WebService 方法。該方法的簽名如下:
List<Item> getInterventions(string, string, string, string, string, string)
Item是自定義定義的類。
當我嘗試檢索getInterventions()VBA Access 代碼的結果時,它會彈出 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 使用與我在 VBA 代碼中傳遞的相同引數直接從 C# 代碼呼叫,并且它作業正常。這證明不是引數或方法內容的問題。
我的結論: 我猜這與我不能簡單地將 C# 物件串列存盤到 VBA 物件中的事實有關。
任何幫助將不勝感激,謝謝。
uj5u.com熱心網友回復:
請自動添加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物件內容。
轉載請註明出處,本文鏈接:https://www.uj5u.com/gongcheng/314218.html
