Skip to content

Instantly share code, notes, and snippets.

@thoriqmacto
Last active January 20, 2021 07:49
Show Gist options
  • Save thoriqmacto/25772061cd3b49058150 to your computer and use it in GitHub Desktop.
Save thoriqmacto/25772061cd3b49058150 to your computer and use it in GitHub Desktop.
Perform HTTP request to certain sites and parse the HTML doc return using VBA with specified class attribute. Must enabled the "Microsoft HTML Object Library" reference.
Public Function GetResponseHTML(ByVal strUrl As String, ByVal className As String, ByVal strDelimiter As Variant, Optional strRespText As String = "", Optional flagVerbose As Boolean = False) As Variant
Dim oHtml As HTMLDocument
Dim oElement As Object
Dim i As Integer
Dim out() As Variant
Set oHtml = New HTMLDocument
With CreateObject("WINHTTP.WinHTTPRequest.5.1")
.Open "GET", strUrl, False
.send
oHtml.body.innerHTML = .responseText
End With
strRespText = oHtml.body.innerHTML
Set strparsed = oHtml.getElementsByClassName(className)
'Debug.Print strParsed.length
i = 0
For Each oElement In strparsed
j = 0
splitter = Split(oElement.outerText, vbCrLf)
ReDim Preserve out(strparsed.length, UBound(splitter))
For Each itm In splitter
out(i, j) = itm
If flagVerbose Then
Debug.Print "i=" & i, "j=" & j, itm
End If
j = j + 1
Next itm
i = i + 1
Next oElement
If flagVerbose Then
Debug.Print ""
Debug.Print "Total Parsed:" & i & vbCrLf & "GetResponseHTML call. Finished."
End If
GetResponseHTML = out
End Function
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment