Help with CreateObject("msxml2.xmlhttp") macro

surfersparadise

New Member
Joined
Feb 23, 2014
Messages
4
Hello,

I'm trying to extract a table Additional Info from the website that is in the code, but failing so far. There is mistake with wrong class name or other variable. Could anyone suggest how to make it work?

Code:
Sub AdditionalInfo()    
    Dim oDom As Object: Set oDom = CreateObject("htmlFile")
    Dim x As Long, y As Long
    Dim oRow As Object, oCell As Object
    Dim data, aTable As Object
    
    y = 1: x = 1
    
    With CreateObject("msxml2.xmlhttp")
        .Open "GET", "http://uk.soccerway.com/matches/2014/02/23/england/premier-league/liverpool-fc/swansea-city-afc/1483721/?ICID=HP_MS_01_01", False
        .send
        oDom.body.innerhtml = .responsetext
    End With
    
    For Each aTable In oDom.getelementsbytagname("table")
        If Trim(LCase(aTable.classname)) = "Additional info" Then
            With aTable
                ReDim data(1 To .Rows.Length, 1 To .Rows(1).Cells.Length)
                For Each oRow In .Rows
                    For Each oCell In oRow.Cells
                        data(x, y) = oCell.innertext
                        y = y + 1
                    Next oCell
                    y = 1
                    x = x + 1
                Next oRow
            End With
            Exit For
        End If
    Next aTable
    


    With Sheets(2).Cells(1, 1)
        .CurrentRegion.ClearContents
        If IsArray(data) Then
            .Resize(UBound(data), UBound(data, 2)).Value = data
        Else: MsgBox "Table not found"
        End If
    End With


End Sub
 

Excel Facts

Easy bullets in Excel
If you have a numeric keypad, press Alt+7 on numeric keypad to type a bullet in Excel.
Hi, try

PHP:
Sub AAA()
Dim mXML As MSXML2.XMLHTTP: Set mXML = New MSXML2.XMLHTTP
Dim O As MSHTML.HTMLDocument: Set O = New HTMLDocument
Dim Coll As HTMLDivElement
Dim p
Dim k As Integer
With mXML
    .Open "GET", "http://uk.soccerway.com/matches/2014/02/23/england/premier-league/liverpool-fc/swansea-city-afc/1483721/?ICID=HP_MS_01_01", False
    .send
    O.body.innerHTML = .responseText
End With
Set Coll = O.getElementById("page_match_1_block_match_additional_info_6")
For k = 0 To Coll.getElementsByTagName("DT").Length - 1
    Range("A" & k + 1) = Coll.getElementsByTagName("DT")(k).innerText
Next
For k = 0 To Coll.getElementsByTagName("DD").Length - 1
    p = Split(Coll.getElementsByTagName("DD")(k).innerText, vbLf)
    Range("B" & k + 1).Resize(1, UBound(p) + 1).Value = p
Next
End Sub
 
Upvote 0
hi, because you need to create early binding, in your VBE editor go to TOOLS then REFERENCES and tick Microsoft XML, v6.0 and also tick Microsoft HTML Object Library then press ok, if you dont have version 6.0 for Microsoft XML any previous version should do
 
Upvote 0
Works superbly. Thank you very much!

Just one more question... If I wanted to extract any other table, info from that site, do I just change elementid or is there something else to change?

Thank you
 
Upvote 0
It depends from the table as it would have a different name and maybe different tags..


You can easily change the destination by changing this part

<code><code>For k = 0 To Coll.getElementsByTagName("DT").Length - 1
Range
("A" & k + 1) = Coll.getElementsByTagName("DT")(k).innerText
Next
For k = 0 To Coll.getElementsByTagName("DD").Length - 1
p
= Split(Coll.getElementsByTagName("DD")(k).innerText, vbLf)
Range("B" & k + 1).Resize(1, UBound(p) + 1).Value = p
Next
</code></code>
 
Upvote 0

Forum statistics

Threads
1,221,525
Messages
6,160,329
Members
451,637
Latest member
hvp2262

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top