3.6.2.7. Invoking Web Services from VBScript Events
This example shows how to configure a VBScript to retrieve a value from a public Web Service:
' This function read a node result from the response XML
Function GetResult(ByVal responseText, ByVal resultParam)
Dim oXml
Set oXml = CreateObject("Msxml2.DOMDocument")
oXml.Async = True
oXml.LoadXml(responseText)
Dim strPath
strPath = "/*/*/*/" + resultParam
Dim oNode
Set oNode = oXml.documentElement.SelectSingleNode(strPath)
GetResult = oNode.Text
End Function
' URL to the WCF service
url= "http://www.webservicex.net/CurrencyConvertor.asmx"
Dim requestDoc
Set requestDoc = CreateObject("MSXML2.DOMDocument.6.0")
Dim root
Set root = requestDoc.createNode(1, "Envelope", "http://schemas.xmlsoap.org/soap/envelope/")
requestDoc.appendChild root
' Create and add Soap params
Dim nodeBody
Set nodeBody = requestDoc.createNode(1, "Body", "http://schemas.xmlsoap.org/soap/envelope/")
root.appendChild nodeBody
Dim nodeOp
Set nodeOp = requestDoc.createNode(1, "ConversionRate", "http://www.webserviceX.NET/")
nodeBody.appendChild nodeOp
Dim nodeRequest1
Dim nodeRequest2
Set nodeRequest1 = requestDoc.createNode(1, "FromCurrency", "http://www.webserviceX.NET/")
Set nodeRequest2 = requestDoc.createNode(1, "ToCurrency", "http://www.webserviceX.NET/")
'content of the request will vary depending On the WCF Service.
'This one takes two parameters, One is Source currency, and other is output currency
nodeRequest1.text = "USD"
nodeRequest2.text = "EUR"
nodeOp.appendChild nodeRequest1
nodeOp.appendChild nodeRequest2
Set nodeRequest = Nothing
Set nodeOp = Nothing
Set nodeBody = Nothing
Set root = Nothing
'the request will look like this:'
' <s:Envelope xmlns:s='http://schemas.xmlsoap.org/soap/envelope/'> '
' <s:Body> '
' <ConversionRate xmlns='urn:Your.Namespace.Here'> '
' <FromCurrency>USD</FromCurrency> '
' <ToCurrency>EUR</ToCurrency> '
' </ConversionRate> '
' </s:Body> '
' </s:Envelope>'
MsgBox "Sending request " & vbCrLf & requestDoc.xml
Dim xmlhttp
Set xmlhttp = CreateObject("MSXML2.ServerXMLHTTP.6.0")
' Set the proxy as necessary and desired, review MSXML2.ServerXMLHTTP Documentation
'xmlhttp.setProxy 2, "PROXYINFO"
xmlhttp.Open "POST", url, False
xmlhttp.setRequestHeader "Content-Type", "text/xml"
' Set SOAPAction as appropriate for the operation
xmlhttp.setRequestHeader "SOAPAction", "http://www.webserviceX.NET/ConversionRate"
' Send the request
xmlhttp.send requestDoc.xml
MsgBox vbCrLf & "Raw XML response:" & vbCrLf
MsgBox xmlhttp.responseXML.xml
' Get the ConversionRateResult result
Dim result
result=GetResult(xmlhttp.responseXML.xml, "ConversionRateResult")
MsgBox result
UserField_Numero_TI.value = result