Error #Value based on excel

0

Good morning

I am creating a formula that when writing a number called RTN, I returned the name after a web query, for example I put the number 08019002275485 in the RTN box and it returns the results of which I need the name "CONSULTING COMPANY IN INGIENERIA CONSULTING S DE RL "

The vba code is as follows

Function Registro(RTN As String)
   Dim IE As Object
   Dim Respuesta As String

   Set IE = CreateObject("InternetExplorer.Application")
    IE.Navigate "http://enlacertn.sar.gob.hn"
    IE.Visible = True
   While IE.busy
    DoEvents
   Wend
   IE.document.All.Item("txtCriterio").Value = RTN
   IE.document.All("btnBuscar").Click
   Application.Wait (Now + TimeValue("0:00:03"))

   Do Until IE.ReadyState = 4
    DoEvents
   Loop
   Respuesta = IE.document.getElementById("LblNombre").innertext
   Registro = Respuesta
  IE.Quit
End Function

I need to extract the element Id = LblName but I returned an error the function

I enclose images for greater understanding

Excel Error

Before the query

After the consultation

code of the website

If you are so kind as to tell me where I have the error in the formula and explain a bit how the code would be to learn.

Thank you!

    
asked by German Rodriguez 19.01.2018 в 12:28
source

1 answer

0

You have to wait for the page to load with While .Busy Or .readyState < 4: DoEvents: Wend . Do not use as a function defined by the user. It is inefficient Call the function from a sub.

Option Explicit
Public Sub test()
    Dim RTN As String
    RTN = "08019002275485"
    Activesheet.Range("A1") =  Registro(RTN)
End Sub

Function Registro(ByVal RTN As String) As String
    Dim IE As Object
    Set IE = CreateObject("InternetExplorer.Application")
    With IE
        .Visible = True
        .navigate "http://enlacertn.sar.gob.hn"

        While .Busy Or .readyState < 4: DoEvents: Wend

        .document.all.item("txtCriterio").Value = RTN
        .document.all("btnBuscar").Click

        While .Busy Or .readyState < 4: DoEvents: Wend

         Registro = .document.getElementById("LblNombre").innerText
        .Quit
    End With
End Function
    
answered by 15.07.2018 в 10:00