Articles → VBA FOR EXCEL → Call Web Service In VBA
Call Web Service In VBA
Sample Web Service
Adding Reference
- Click on ‘Tools – References…’.
Click to Enlarge
- A window will appear to add references. Select ‘Microsoft.XML, V3.0’.
Click to Enlarge
Code
Dim sMsg As String
Dim sURL As String
Dim sEnv As String
Set ObjHTTP = New MSXML2.XMLHTTP
sURL = "http://www.dneonline.com/calculator.asmx?op=Add"
sEnv = "<?xml version=""1.0"" encoding=""utf-8""?>"
sEnv = sEnv & "<soap12:Envelope xmlns:xsi=""http://www.w3.org/2001/XMLSchema-instance"" xmlns:xsd=""http://www.w3.org/2001/XMLSchema"" xmlns:soap12=""http://www.w3.org/2003/05/soap-envelope"">"
sEnv = sEnv & "<soap12:Body>"
sEnv = sEnv & "<Add xmlns=""http://tempuri.org/"">"
sEnv = sEnv & "<intA>1</intA>"
sEnv = sEnv & "<intB>2</intB>"
sEnv = sEnv & "</Add>"
sEnv = sEnv & "</soap12:Body>"
sEnv = sEnv & "</soap12:Envelope>"
ObjHTTP.Open "Post", sURL, False
ObjHTTP.setRequestHeader "Content-Type", "text/xml"
ObjHTTP.setRequestHeader "Action", "http://tempuri.org/Add"
ObjHTTP.Send (sEnv)
MsgBox ObjHTTP.responseText
Set ObjHTTP = Nothing
Set xmlDoc = Nothing
Output
Click to Enlarge