Skip to content

vba - HTMLDocument variable issue making my head spin #277

@shahin2137

Description

@shahin2137

#Hi there! This is my first post and i'm totally new here so apology for any mistakes. I'm seriously worried about untangling a complex issue concerning when to use the same HTMLDocument variable in a single subroutine for all the HTTP requests response processing and when I go for using different HTMLDocument variable, but can't get any solution. Because using the different HTMLDocument variables in some cases I get bunch of duplicate results, and using the same one I get run time error 91. Are there hard and fast rules on using it? For your consideration I pasted here the code I collected and modified to fit in a single subroutine. It works very fine but before sending the first HTTP request I used html, and for the second and third HTTP request i used hmm as htmldocument variable. Why they are not to be the same or to be the different always. BTW, if the HTMLDocument variables used in the code i pasted here are #applied somewhat differently i meant first two are identical or first and last one are identical then it will definitely come up with messy results. Thanks in advance.

Const Pageurl As String = "http://www.wiseowl.co.uk/videos/"
Sub parsehtml()

Dim http As New MSXML2.XMLHTTP60, html As New HTMLDocument, hmm As New HTMLDocument
Dim topics As Object, gist As Object
Dim x As String, y As String, z As String
Dim vid As Integer, vidcat As Object
Dim vrow As Object, vrows As Object, m As String, n As String
Dim vlink As Object, find As Object, q As Integer
Dim t As String, r As String, s As String, L As String

Range("A2").Select

http.Open "GET", Pageurl, False
http.send
html.body.innerHTML = http.responseText
Set http = Nothing

' Look up and down to find "html" variable

Set topics = html.getElementsByClassName("woMenuList")(0)
Set gist = topics.getElementsByTagName("a")

For vid = 1 To gist.Length - 1
    Set vidcat = gist(vid)
    x = vidcat.getAttribute("href")
    z = Pageurl & Mid(x, InStr(x, ":") + 9)

    http.Open "GET", z, False
    http.send
    hmm.body.innerHTML = http.responseText
    Set http = Nothing

    ' Look up and down to find "hmm" variable

    Set find = hmm.getElementsByClassName("woPagingItem")
    For q = 0 To find.Length - IIf(find.Length > 0, 1, 0)
        If q > 0 Then
            t = find(q).innerText
            r = find(q).getAttribute("href")
            s = Pageurl & Mid(r, InStr(r, ":") + 9)

            http.Open "GET", s, False
            http.send
            hmm.body.innerHTML = http.responseText
            Set http = Nothing
        End If

        ' Look up and down to find "hmm" variable

        Set vrows = hmm.getElementsByClassName("woVideoListRow")
        For Each vrow In vrows
            Set vlink = vrow.getElementsByTagName("a")(0)
            m = vlink.getAttribute("href")
            L = Pageurl & Mid(m, InStr(m, ":") + 9)
            n = vlink.innerText
            ActiveCell.Value = n
            ActiveCell.Offset(0, 1) = L
            ActiveCell.Offset(1, 0).Select
        Next vrow
    Next q
Next vid

End Sub

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions