Collect HTML tag data with VBA

Asked

Viewed 2,583 times

0

How can I get the return of an html tag through getElementById? I’m testing in the application below, but I’m not getting it.

My intention is to catch what’s inside the tag.

Sub teste()

Dim IE As Object
Set IE = CreateObject("internetexplorer.application")
IE.Visible = True
IE.Navigate "https://web.whatsapp.com/"
Do While IE.Busy
Loop
Dim oHTMLdoc As Object
Set oHTMLdoc = IE.Document
Dim teste As String
teste = oHTMLdoc.getElementById("wrapper")
If teste = "" Then
MsgBox "ok"
End If


End Sub
  • If what you want is inside class="app-wrapper app-wrapper-web", you need to use the getElementByClassName.

  • Even using "getElementByClassName" it keeps giving error 438.

1 answer

0

The Whatsapp does not run with the Internetexplorer, therefore use Set IE = CreateObject("internetexplorer.application") will not work because when opening the page in IE, the following screen appears:

Tela no Internet Explorer

IMPORTANT: You must use the latest version of Chrome, Firefox, Opera, Safari or Edge browsers in order to use Whatsapp Web. Other browsers like Internet Explorer do not support the application.

Why can’t I connect to Whatsapp Web?

You can use in other browsers, but you will need to use other references. As for example this API or use another program, such as Selenium.

To automate other browsers, there are numerous applications and software to accomplish this.

If you want to see the html code of the site and save it, use the following code:

Option Explicit
#If VBA7 Then
Declare PtrSafe Sub Sleep Lib "kernel32" (ByVal dwMilliseconds As LongPtr)
#Else
Declare Sub Sleep Lib "kernel32" (ByVal dwMilliseconds As Long)
#End If

Sub PraticarInternetExplorer()


    Dim IE As InternetExplorer
    Dim HTMLDoc As HTMLDocument
    Dim teste As Object
    Dim n As Long
    Set IE = New InternetExplorer                'Set IE = CreateObject("InternetExplorer.Application")
    Dim sFilename As String, sFilepath As String
    Dim objStream As Object
    Dim strData As String
    Set objStream = CreateObject("ADODB.Stream")
    sFilename = "temp.txt"
    sFilepath = ThisWorkbook.Path & "\" & sFilename

    With IE
        .Silent = True
        .Visible = True
        .Navigate "https://web.whatsapp.com/"
    End With

    WaitIE IE, 5000

    Set HTMLDoc = IE.Document
    '    Debug.Print HTMLDoc.DocumentElement.innerText

    objStream.Type = 2                           'Specify stream type - we want To save text/string data.
    objStream.Charset = "utf-8"                  'Specify charset For the source text data.
    objStream.Open                               'Open the stream And write binary data To the object
    objStream.WriteText HTMLDoc.DocumentElement.innerHTML
    objStream.SaveToFile sFilepath, 2            'Save binary data To disk
    'close down IE and reset status bar
    objStream.Close

    IE.Quit
    Set IE = Nothing
End Sub

Sub WaitIE(IE As Object, Optional time As Long = 250)
'Code from: https://stackoverflow.com/questions/33808000/run-time-error-91-object-variable-or-with-block-variable-not-set
Dim i As Long
Do
    Sleep time
    Debug.Print CStr(i) & vbTab & "Ready: " & CStr(IE.READYSTATE = 4) & _
                vbCrLf & vbTab & "Busy: " & CStr(IE.Busy)
    i = i + 1
Loop Until IE.READYSTATE = 4 Or Not IE.Busy
End Sub
  • I understood your code. The challenge is to read what the object is returning or to assign it to a cell in the spreadsheet itself in excel. Can you give any hint about?

  • For the Whatsapp website this is impossible with Internetexplorer, if for another website, could you ask another question on Sopt? The way this example code works, is by creating a file .txt. Because cells have very small character limits, such as the Debug.Print. That is, often the code is cut. And also the visualization in Notepad++ facilitates the understanding of HTML code.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.