Search code examples
excelvbaxml-parsingwinhttprequest

Parse line from HTML response in Excel VBA


I am looking to parse a 'WinHttpRequest' response in Excel VBA to pull a specific line from the HTML response. Here is the code for the HTTP request.

Function getSetName(ByVal setNUMBER As String) As String
    Dim oRequest As Object
    Dim xmlResponse As String
    Dim setDescription As String
    Dim setName As String
   

    Set oRequest = CreateObject("WinHttp.WinHttpRequest.5.1")

    oRequest.Open "GET", "https://brickset.com/sets/75192" '& setNUMBER
    oRequest.Send
    xmlResponse = oRequest.ResponseText
    
    'parse xml response here
    
    getSetName = setName

End Function

I am looking to parse only the line with the HTML tag 'meta name=""description""' to a string which I will later pull information from.

Any help or direction on how to parse this single line would be appreciated.


Solution

  • Try

    Sub Test_GetSetName()
        Debug.Print GetSetName("75192")
    End Sub
    
    Function GetSetName(ByVal SetNUMBER As String) As String
        Dim html As New MSHTML.HTMLDocument
        With CreateObject("WinHttp.WinHttpRequest.5.1")
            .Open "GET", "https://brickset.com/sets/" & SetNUMBER, False
            .send
            html.body.innerHTML = .responseText
        End With
        GetSetName = html.querySelector("[name='description']").Content
    End Function