MSXML http request produces different result than IE object



  • There are many cases where I use the IE object for test purpose and then switch to MSXML for production. I understand that these should produce identical responses but for some reason, the following code produces two different responses.

    Sub testHTTP()
        Dim link As String
        link = "https://www.govtrack.us/congress/members/ralph_abraham/412630"
    
    'THIS IS THE XML SECTION
        Dim xmlHTMLDoc As HTMLDocument
        Dim xmlWeb As msxml2.XMLHTTP60
        Set xmlHTMLDoc = New HTMLDocument
        Set xmlWeb = New msxml2.XMLHTTP60
        
        xmlWeb.Open "GET", link, False
        xmlWeb.send
        
        While xmlWeb.readyState <> 4
            DoEvents
        Wend
        
        Debug.Print " "
        Debug.Print link
        Debug.Print xmlWeb.Status; "XMLHTTP status "; xmlWeb.statusText; " at "; Time
        
        xmlHTMLDoc.body.innerHTML = xmlWeb.responseText
        Debug.Print "MSXML response finds image tag at position: " & InStr(xmlWeb.responseText, "img")
        Debug.Print "MSXML response getElementsByTagName(img).Length is: " & xmlHTMLDoc.getElementsByTagName("img").Length
        
    'THIS IS THE IE SECTION
        Dim ieHTMLDoc As HTMLDocument
        Dim objIE As Object
        Set ieHTMLDoc = New HTMLDocument
        Set objIE = CreateObject("InternetExplorer.Application")
        
        With objIE
            .Top = 0
            .Left = 600
            .Width = 800
            .Height = 600
            .Visible = False
        End With
        
        objIE.navigate (link)
        While objIE.readyState <> 4
            DoEvents
        Wend
        
        If objIE.readyState = 4 Then
            Set ieHTMLDoc = objIE.document
            Debug.Print "IE response getElementsByTagName(img).Length is: " & ieHTMLDoc.getElementsByTagName("img").Length
        End If
    End Sub
    

    Here are the results from the immediate window:

    https://www.govtrack.us/congress/members/ralph_abraham/412630
    200 XMLHTTP status OK at 7:50:44 PM 
    MSXML response finds image tag at position: 8936
    MSXML response getElementsByTagName(img).Length is: 0
    IE response getElementsByTagName(img).Length is: 10
    

    Here's another example, this time trying to find anchor links:

    Sub testHTTP()
        Dim link As String
        link = "https://www.govtrack.us/congress/members/ralph_abraham/412630"
    
    'THIS IS THE XML SECTION
        Dim xmlHTMLDoc As HTMLDocument
        Dim xmlWeb As msxml2.XMLHTTP60
        Set xmlHTMLDoc = New HTMLDocument
        Set xmlWeb = New msxml2.XMLHTTP60
        
        xmlWeb.Open "GET", link, False
        xmlWeb.send
        
        While xmlWeb.readyState <> 4
            DoEvents
        Wend
        
        Debug.Print " "
        Debug.Print link
        Debug.Print xmlWeb.Status; "XMLHTTP status "; xmlWeb.statusText; " at "; Time
        
        xmlHTMLDoc.body.innerHTML = xmlWeb.responseText
        Debug.Print "MSXML response finds anchor tag at position: " & InStr(xmlWeb.responseText, "<a ")
        Debug.Print "MSXML response getElementsByTagName(<a ).Length is: " & xmlHTMLDoc.getElementsByTagName("a").Length
        
    'THIS IS THE IE SECTION
        Dim ieHTMLDoc As HTMLDocument
        Dim objIE As Object
        Set ieHTMLDoc = New HTMLDocument
        Set objIE = CreateObject("InternetExplorer.Application")
        
        With objIE
            .Top = 0
            .Left = 600
            .Width = 800
            .Height = 600
            .Visible = False
        End With
        
        objIE.navigate (link)
        While objIE.readyState <> 4
            DoEvents
        Wend
        
        If objIE.readyState = 4 Then
            Set ieHTMLDoc = objIE.document
            Debug.Print "IE response getElementsByTagName(<a ).Length is: " & ieHTMLDoc.getElementsByTagName("a").Length
        End If
    End Sub
    

    Here is the immediate window:

    https://www.govtrack.us/congress/members/ralph_abraham/412630
    200 XMLHTTP status OK at 12:21:08 PM 
    MSXML response finds anchor tag at position: 3774
    MSXML response getElementsByTagName(<a ).Length is: 0
    IE response getElementsByTagName(<a ).Length is: 131
    

    Here is the method that blows up the code:

    getElementsByClassName("photo")(0).getElementsByTagName("img")(0).src    
    

    This produces an "object variable or with block variable not set" error when run against the XML request response but not the IE response. It looks like the XML response has everything in it but is not being interpreted properly as an HTMLDocument object.

    I need to know how to substitute XML or some other http method in vba for IE.


Log in to reply
 

Looks like your connection to Codingislove Forum was lost, please wait while we try to reconnect.