excelvbams-accessweb-scraping

access vba bombs when defining html results page as variable to extract content from php page


My client is trying to use an access VBA script to send a string to a php page i created, then bring the data that is returned on the page back into his db

i use a mac and cannot run any of the VB code, but here is what i was able to find (two versions), but both bomb in the first dim statement

Private Sub Command1_Click()

Dim iHTML As HTMLDocument
Dim objHttp As MSXML2.ServerXMLHTTP
set objHttp = CreateObject("Msxml2.ServerXMLHTTP")
objHttp.open "GET", "http://www.bestenergyctri.com/zipcode.php?isValidate=adb&address1=352%20w%2046&address2=&city=new%20york&state=ny&zip5= 
", False
objHttp.send

Set iHTML = objHttp.ResponseText

straddress1 = iHTML.getElementsByClassName("detect").Item(x - 1).getElementsByClassName("thedata").Item(0).getElementsByClassName("address1").Item(1).innerText
straddress2 = iHTML.getElementsByClassName("detect").Item(x - 1).getElementsByClassName("thedata").Item(0).getElementsByClassName("address2").Item(1).innerText
strcity = iHTML.getElementsByClassName("detect").Item(x - 1).getElementsByClassName("thedata").Item(0).getElementsByClassName("City").Item(1).innerText
strstate = iHTML.getElementsByClassName("detect").Item(x - 1).getElementsByClassName("thedata").Item(0).getElementsByClassName("State").Item(1).innerText
strzip5 = iHTML.getElementsByClassName("detect").Item(x - 1).getElementsByClassName("thedata").Item(0).getElementsByClassName("Zip5").Item(1).innerText
strzip4 = iHTML.getElementsByClassName("detect").Item(x - 1).getElementsByClassName("thedata").Item(0).getElementsByClassName("Zip4").Item(1).innerText

SaveWebInfo straddress1, straddress2, strcity, strstate, strzip5, strzip4
Set iHTML = Nothing
Set objHttp = Nothing

End Sub

or

Private Sub Command1_Click()

Dim iHTML As HTMLDocument
Dim objHttp As MSXML2.ServerXMLHTTP
set objHttp = New MSXML2.ServerXMLHTTP
objHttp.open "GET", "http://www.bestenergyctri.com/zipcode.php?isValidate=adb&address1=352%20w%2046&address2=&city=new%20york&state=ny&zip5= 
", False
objHttp.send

Set iHTML = objHttp.ResponseText

straddress1 = iHTML.getElementsByClassName("detect").Item(x - 1).getElementsByClassName("thedata").Item(0).getElementsByClassName("address1").Item(1).innerText
straddress2 = iHTML.getElementsByClassName("detect").Item(x - 1).getElementsByClassName("thedata").Item(0).getElementsByClassName("address2").Item(1).innerText
strcity = iHTML.getElementsByClassName("detect").Item(x - 1).getElementsByClassName("thedata").Item(0).getElementsByClassName("City").Item(1).innerText
strstate = iHTML.getElementsByClassName("detect").Item(x - 1).getElementsByClassName("thedata").Item(0).getElementsByClassName("State").Item(1).innerText
strzip5 = iHTML.getElementsByClassName("detect").Item(x - 1).getElementsByClassName("thedata").Item(0).getElementsByClassName("Zip5").Item(1).innerText
strzip4 = iHTML.getElementsByClassName("detect").Item(x - 1).getElementsByClassName("thedata").Item(0).getElementsByClassName("Zip4").Item(1).innerText

SaveWebInfo straddress1, straddress2, strcity, strstate, strzip5, strzip4
Set iHTML = Nothing
Set objHttp = Nothing

End Sub

does anyone have any suggestions on how we can get the page properly read into a variable so that it can be parsed

thanks


Solution

  • Here is a simpler example using CSS querySelector and avoiding using Hungarian notation

    Code:

    Option Explicit
    
    Sub test()
    
        Dim iHTML As New HTMLDocument, objHttp As MSXML2.ServerXMLHTTP60 '<== Note this is version specific syntax. 60 is for Excel 2016
        Set objHttp = New MSXML2.ServerXMLHTTP60
        objHttp.Open "GET", "http://www.bestenergyctri.com/zipcode.php?isValidate=adb&address1=352%20w%2046&address2=&city=new%20york&state=ny&zip5=", False
        objHttp.send
    
        Dim address1  As String, address2 As String, city As String, state As String, zip5 As String, zip4 As String
        With iHTML
            .body.innerHTML = objHttp.ResponseText
            address1 = .querySelector(".address1").innerText
            address2 = .querySelector(".address2").innerText
            city = .querySelector(".City").innerText
            state = .querySelector(".State").innerText
            zip5 = .querySelector(".Zip5").innerText
            zip4 = .querySelector(".Zip4").innerText
        End With
    
        Debug.Print "Address 1: " & address1
        Debug.Print "Address 2: " & address2
        Debug.Print "City: " & city
        Debug.Print "State: " & state
        Debug.Print "Zip5: " & zip5
        Debug.Print "Zip4: " & zip4
    End Sub
    

    Output:

    Example output

    References added to VBA > Tools > References:

    References

    *Last two references are the important ones.