Search This Blog

Tuesday, September 17, 2019

Excel VBA - Loading POST REST Webservice JSON

1. Open excel -> developer -> visual basic
2. Tools -> reference (enabled : Microsoft win http services , version 5.1 xx )
3. paste the below code on click sheet1.
4. Click run.
5. Save As excel macro enabled from file type . else your VBA code will not be save.

Dim strResult As String
Dim objHTTP As Object
Dim URL As String
Private Sub Worksheet_Activate()
    Set objHTTP = CreateObject("WinHttp.WinHttpRequest.5.1")
    json_body = "{'Commodity':'CRUDEOIL','Expiry':'17SEP2019'}"
    URL = ""
    objHTTP.Open "POST", URL, False
    objHTTP.SetRequestHeader "User-Agent", "Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.0)"
    objHTTP.SetRequestHeader "Content-type", "application/json"
    objHTTP.Send (json_body)
    strResult = objHTTP.ResponseText
    Worksheets("Sheet1").Range("A10:A10") = strResult
End Sub

No comments:

Hit Counter

View My Stats