3

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 !

2
  • 2
    Why do you set xobj twice before line result = xobj.innerText? Also, are you sure about Set xobj = xobj.getElementsByTagName("td").Item(0)? Are you sure it does not start from Item(1)?
    – simpLE MAn
    Feb 10, 2014 at 21:47
  • @simpLE MAn No I am not sure. My problem is that I can acess to the td name but I would like to access to the next td and there is no reference. So if I let Set xobj =IE.Document.getElementById("vatResponseFormTable").getElementsByClassName("labelStyle").Item(3) , then the return is "Name". I would like to have the next text , for example "Company ABC".... Feb 25, 2014 at 9:11

1 Answer 1

2

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
4
  • Hi, Thank you very much for your answer (sorry I have just seen it now. I will now try and let you know. Feb 17, 2014 at 14:05
  • 1
    I have a error message: error code 438 - Object does not support property or method. This error come on the line [Msgbox MsgBox "Name : " & td.NextSibling.innerText Feb 25, 2014 at 8:41
  • @user3294649 I tested this code and it works for me. Can you give it a try again. Also can you let me know which IE version you have.
    – Santosh
    Mar 3, 2014 at 9:46
  • Have you received my comment (not sure how to send you a message)? Thank you – Mar 17, 2014 at 14:05

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.