Search code examples
vbawebbrowser-control

How do I get the information for an HTML element via VBA and the web browser control?


I have a question. I have a webbrowsercontrol and want to read different data from a webpage via vba.

The data is as follows:

<article class="messagexyz " data-author="mrjemand" data-content="something" id="1305195" ...

Using the following code:

For Each All In ebform!WebBrowser0.Document.getElementsByTagName("article")

I get my elements. But how do I get the information from data-author,data-content and id. Unfortunately I cannot find the appropriate program code.

Thank you very much !

I tried it with getAttribute and getProperty because I couldn't come up with anything sensible.


Solution

  • For example:

    Private Sub UserForm_Activate()
        Dim art
        
        Me.web.Navigate "about:blank" ' `web` is a web browser control
        Do While Me.web.Busy
            DoEvents
        Loop
        
        With Me.web.Document
            .Open
            'create some article content 
            .Write "<article class='messagexyz' data-author='mrjemand' data-content='something' id='1305195'>Article here</article>"
            .Write "<article class='messageabc' data-author='Jim' data-content='blah' id='1305196'>Article2 here</article>"
            .Close
        End With
        'loop each article
        For Each art In Me.web.Document.getElementsByTagName("article")
            Debug.Print art.innerHTML
            'list some attributes
            Debug.Print , art.getAttribute("data-author")
            Debug.Print , art.getAttribute("class")
            Debug.Print , art.getAttribute("data-content")
        Next art
        
    End Sub
    

    output:

    Article here
                  mrjemand
                  messagexyz
                  something
    Article2 here
                  Jim
                  messageabc
                  blah