Hi, i am just after some information, i use a couple different codes for extracting webpages and am just wanting someone to explain the differences and why one would be better used over the other.
Code:
Public Function ExecuteWebRequest(URL As String) As String
Dim oXHTTP As Object
Set oXHTTP = CreateObject("MSXML2.XMLHTTP")
oXHTTP.Open "GET", URL, False
oXHTTP.Send
ExecuteWebRequest = oXHTTP.responseText
Set oXHTTP = Nothing
End Function
Code:
Public Function outputtext(Text As String)
Dim MyFile As String, fnum As String
MyFile = ThisWorkbook.Path & "\temp.txt"
fnum = FreeFile()
Open MyFile For Output As fnum
Print #fnum, Text
Close #fnum
End Function
Code:
Sub WebQuery()
Application.ScreenUpdating = False
Sheets("Sheet2").Select
Range("A1:Z500").Value = ""
Sheets("sheet2").Range("A1").Value = Sheets("sheet1").Range("A8").Value
formhtml = ExecuteWebRequest(ThisWorkbook.Sheets("Sheet2").Range("A1").Value)
outputtext (formhtml)
Set temp_qt = ThisWorkbook.Sheets("Sheet2").QueryTables.Add(Connection:= _
"URL;" & ThisWorkbook.Path & "\temp.txt" _
, Destination:=ThisWorkbook.Sheets("Sheet2").Range("$A$2"))
With temp_qt
.RefreshStyle = xlOverwriteCells
'.WebSelectionType = xlEntirePage
.WebSelectionType = xlSpecifiedTables
.WebFormatting = xlWebFormattingNone
.WebTables = "2"
.SaveData = True
.Refresh BackgroundQuery:=False
End With
ActiveSheet.QueryTables.Item(1).Delete
Set temp_qt = Nothing
Kill ThisWorkbook.Path & "\temp.txt"
For Each ws In ThisWorkbook.Worksheets
For Each qt In ws.QueryTables
qt.Delete
Next qt
Next ws
If ActiveWorkbook.Connections.Count > 0 Then
For i = 1 To ActiveWorkbook.Connections.Count
ActiveWorkbook.Connections.Item(1).Delete
Next i
End If
End Sub
Code:
Sub ObjWeb()
Application.ScreenUpdating = False
Sheets("Sheet2").Select
Range("A1:Z500").Value = ""
Sheets("sheet2").Range("A1").Value = Sheets("sheet1").Range("A8").Value
Dim ObjWeb As QueryTable
Set ObjWeb = ActiveSheet.QueryTables.Add(Connection:="URL;" & ThisWorkbook.Sheets("Sheet2").Range("A1").Value, _
Destination:=Range("$A$1"))
With ObjWeb
.RefreshStyle = xlOverwriteCells
.WebSelectionType = xlEntirePage
.WebFormatting = xlWebFormattingNone
.WebDisableDateRecognition = True
.SaveData = True
.Refresh BackgroundQuery:=False
End With
Application.ScreenUpdating = True
For Each ws In ThisWorkbook.Worksheets
For Each qt In ws.QueryTables
qt.Delete
Next qt
Next ws
If ActiveWorkbook.Connections.Count > 0 Then
For i = 1 To ActiveWorkbook.Connections.Count
ActiveWorkbook.Connections.Item(1).Delete
Next i
End If
End Sub
Last edited: