Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to read a IE table text with VBA?

I am trying to write a vba code in order to follow this process:

  1. Automatic Web form fill in and submitting (a new web page opens with the answer http://ec.europa.eu/taxation_customs/vies/?locale=en)

  2. Find the address of the new webpage (because i will need to read this content)

  3. Read a specific cell of the html table (the name of the company)

You can try to manualy submit the for with the VAT number FR(FRANCE) and 27435044714. It will return a page including the name of the company.

Basically I am struggling to extract the name of the company.

Step 1 works very well and is independent from step 2 & 3. Step 2 and step 3 are on a same Sub:

The macro starts with the step 2 (finding the opened webpage)

Sub recup()
Dim SWs As SHDocVw.ShellWindows, IE As SHDocVw.InternetExplorer 
'Establish link to IE application
Set SWs = New SHDocVw.ShellWindows

For Each IE In SWs
    If Left(IE.LocationURL, 4) <> "http" Then
    GoTo autre
    End If
    Address = IE.LocationURL
    GoTo vabene 'avoid explorer windows/etc this way
autre:
Next
vabene:

Then, I proceed to the step 3 in order to extract the text.

IE.Visible = True
Dim xobj
Set IE = CreateObject("InternetExplorer.Application")
IE.Visible = True
Set xobj =IE.Document.getElementById("vatResponseFormTable").getElementsByClassName("labelStyle").Item(3)
Set xobj = xobj.getElementsByTagName("td").Item(0)
result = xobj.innerText
Set xobj = Nothing
IE.Quit
Set IE = Nothing
End Sub

My problem: the macro stops on the line (Runtime error 91):

result = xobj.innerText

This seems to come from the previous line

Set xobj = xobj.getElementsByTagName("td").Item(0)

I search a lot into the web and this forum (it helped me until this step). If you can help me that would save my week !

like image 341
user3294649 Avatar asked Dec 05 '25 04:12

user3294649


1 Answers

Try this

Sub getData()

'~~~~Variable declaration~~~~'
    Dim IE As Object
    Dim country As Object
    Dim num As Object
    Dim btn As Object
    Dim tlb As Object, td As Object

    Set IE = CreateObject("InternetExplorer.Application")

    IE.Visible = False
    IE.navigate "http://ec.europa.eu/taxation_customs/vies/?locale=en"

'Wait till page is loaded
    Do While IE.readystate <> 4
        DoEvents
    Loop


    Set country = IE.document.getelementbyid("countryCombobox")
    country.Value = "FR" 'set the value for Member state


'Pause the code for 5 sec
    Application.Wait Now + TimeSerial(0, 0, 5)

'
    Set num = IE.document.getelementbyid("number")
    num.Value = "27435044714" 'set the Vat number


    Application.Wait Now + TimeSerial(0, 0, 5)


    Set btn = IE.document.getelementbyid("submit")
    btn.Click ' click the verify button

'Wait till page is loaded
    Do While IE.readystate <> 4: DoEvents: Loop

'Pause the code for 5 sec
        Application.Wait Now + TimeSerial(0, 0, 10)

        Set tbl = IE.document.getelementbyid("vatResponseFormTable")

        For Each td In tbl.getelementsbytagname("td")
            If td.innerText = "Name" Then
                MsgBox "Name : " & td.NextSibling.innerText
            ElseIf td.innerText = "Address" Then
                MsgBox "Address : " & td.NextSibling.innerText
            ElseIf td.innerText = "Consultation Number" Then
                MsgBox "Consultation Number : " & td.NextSibling.innerText
            End If

        Next


        IE.Quit
        Set IE = Nothing
 End Sub
like image 161
Santosh Avatar answered Dec 07 '25 22:12

Santosh