Saturday, June 14, 2008

VB/VBA Call WebService

You can call a webservice from VBA simply by posting the soap envelope. Here is an example:
    Dim http As New WinHttp.WinHttpRequest
Dim URL As String
Dim envelope As String
URL = "http://notificationserver/NotificationServer/NotifyService"
envelope = "<?xml version=""1.0"" encoding=""UTF-8""?><soap:Envelope soap:encodingStyle=""http://schemas.xmlsoap.org/soap/encoding/"" xmlns:soap=""http://schemas.xmlsoap.org/soap/envelope/"" xmlns:soapenc=""http://schemas.xmlsoap.org/soap/encoding/"" xmlns:xsi=""http://www.w3.org/2001/XMLSchema-instance"" xmlns:xsd=""http://www.w3.org/2001/XMLSchema"" xmlns:tns=""http://NotificationServer.nvidia.com/""><soap:Body><tns:notify><id xsi:type=""xsd:string"">blah</id><from xsi:type=""xsd:string"">from </from><subject xsi:type=""xsd:string"">subject</subject><details xsi:type=""xsd:string"">details</details></tns:notify></soap:Body></soap:Envelope>"

Call http.Open("POST", URL)

http.SetRequestHeader "Content-Type", "text/xml"
http.SetRequestHeader "SOAPAction", " "

http.Send envelope
MsgBox http.ResponseText

No comments: