我已经通过 WCF Web 服务创建了一个方法。我已将其上传到我的服务器。我想做的是在 Excel VBA 宏中调用该方法。可能吗?
类似于:
Dim client As DaybookServicesClient = New DaybookServicesClient()
' Use the 'client' variable to call operations on the service.
' Calls my method
client.ExecuteSQLJob()
' Always close the client.
client.Close()
如何在 Excel VBA 中引用我的服务?
最佳答案
一种方法可以创建 WebGet wcf 服务,该服务将返回 adorecordset xml,在宏上您可以使用以下代码获取记录集
Public Function GetRSFromString(sXML As String) As Object
Dim oStream As Object, oRecordset As Object
Set oStream = CreateObject("ADODB.Stream")
oStream.Open
oStream.WriteText sXML
oStream.Position = 0
Set oRecordset = CreateObject("ADODB.Recordset")
oRecordset.Open oStream
oStream.Close
Set oStream = Nothing
Set GetRSFromString = oRecordset
Set oRecordset = Nothing
End Function
Public Function GetSoapRequest()
Dim strResult As String
Dim xmlhtp As Object, xmlDoc As Object, oRecordSetFromXML As Object
Set xmlhtp = CreateObject("msxml2.xmlhttp.6.0")
With xmlhtp
.Open "get", "http://[server]/ServiceName.svc/FunctionName", False
.setRequestHeader "Content-Type", "application/x-www-form-urlencoded"
.Send
Set xmlDoc = CreateObject("msxml2.DOMDocument.6.0")
strResult = .responseText
xmlDoc.loadXML strResult
Set oRecordSetFromXML = AdoFunction .GetRSFromString(xmlDoc.Text)
End With
Set xmlDoc = Nothing
Set xmlhtp = Nothing
End Function
关于wcf - 通过 Excel VBA 从 WCF 服务调用方法,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11154896/