Forum Discussion

Malc_B's avatar
Malc_B
New Member
6 years ago

Passing details to a webform

I currently have the following VBA code in Excel that sends the contents of 2 cells to a web page form and gets the results back. Problem is some of it is in pretty dire shape so I was hoping I could use power query to transform it before it gets back to the sheet. Problem is I am no code with MCode (this would be about my first foray into the murky depths) so I'm a bit lost.

 

The current VBA code is:

Sub BasicPOSTRequest()
Dim req As New MSXML2.XMLHTTP60
Dim reqURL As String
Dim HTMLDoc As New MSHTML.HTMLDocument

'Dim HTMLTables As MSHTML.IHTMLElementCollection

reqURL = "http://ec.europa.eu/taxation_customs/vies/vatResponse.html"

req.Open "POST", reqURL, False

req.setRequestHeader "Content-Type", "application/x-www-form-urlencoded"
req.send "memberStateCode=" & WorksheetFunction.EncodeURL(ThisWorkbook.Worksheets("input").[n5]) & _
"&number=" & WorksheetFunction.EncodeURL(ThisWorkbook.Worksheets("input").[i5])

If req.Status <> 200 Then
        MsgBox req.Status & " - " & req.statusText
        Exit Sub
        
End If
'Set HTMLTables = HTMLDoc.getElementsByTagName("Table")
HTMLDoc.body.innerHTML = req.responseText

'Debug.Print HTMLTables.Length

ProcessHTMLPage HTMLDoc
End Sub


Sub ProcessHTMLPage(HTMLPage As MSHTML.HTMLDocument)
Dim HTMLTable As MSHTML.IHTMLElement
Dim HTMLTables As MSHTML.IHTMLElementCollection
Dim HTMLRow As MSHTML.IHTMLElement
Dim HTMLCell As MSHTML.IHTMLElement
Dim rownum As Long, colnum As Integer

Set HTMLTables = HTMLPage.getElementsByTagName("table")

For Each HTMLTable In HTMLTables
rownum = 1


    For Each HTMLRow In HTMLTable.getElementsByTagName("tr")
    colnum = 1
        For Each HTMLCell In HTMLRow.Children       'getElementsByTagName("td")
        ThisWorkbook.Worksheets("xxx_process").Cells(rownum, colnum) = HTMLCell.innerText
        colnum = colnum + 1
            
        Next HTMLCell
        rownum = rownum + 1
     
    Next HTMLRow
    


Next HTMLTable


End Sub

I can connect to the page easily enough (the results page) but it won't give me any useful details without getting the details in n5 and i5. Firstly is it possible to use the contents of a cell to be used in thew request and secondly how do I get it to post to the site.

 

Thanks for the help.

1 Reply

  • v-frfei-msft's avatar
    v-frfei-msft
    Icon for Community Support rankCommunity Support

    Hi Malc_B ,

     

    To my knowledge, we cannot send data to web in power query. We can only extract data from web.