Collecting HTML tag data with VBA

0

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

My intention is to get 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
    
asked by anonymous 09.11.2017 / 18:45

1 answer

0

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

  

IMPORTANT:YoumustusethelatestversionofChrome,Firefox,Opera,SafariorEdgebrowserstouseWhatsAppWeb.OtherbrowserssuchasInternetExplorerdonotsupporttheapplication.

Why can not I connect to the WhatsApp Web?

You can use it in other browsers, however you will need to use other references. Like 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
    
13.11.2017 / 13:52