2017-05-31 1 views
-3
'start a new subroutine called SearchBot 

Sub SearchBot() 

'dimension (declare or set aside memory for) our variables 
Dim objIE As InternetExplorer 'special object variable representing the IE browser 
Dim aEle As HTMLLinkElement 'special object variable for an <a> (link) element 
Dim y As Integer 'integer variable we'll use as a counter 
Dim result As String 'string variable that will hold our result link 



Dim x As Integer 
    Application.ScreenUpdating = False 
    ' Set numrows = number of rows of data. 
    NumRows = Range("A1", Range("A1").End(xlDown)).Rows.Count 
    ' Select cell a1. 
    Range("A1").Select 
    ' Establish "For" loop to loop "numrows" number of times. 
    For x = 1 To NumRows 
    ' Insert your code here. 

    'initiating a new instance of Internet Explorer and asigning it to objIE 
Set objIE = New InternetExplorer 

'make IE browser visible (False would allow IE to run in the background) 
objIE.Visible = True 

'navigate IE to this web page (a pretty neat search engine really) 
objIE.navigate "http://ec.europa.eu/taxation_customs/vies/vatResponse.html" 

'wait here a few seconds while the browser is busy 
Do While objIE.Busy = True Or objIE.readyState <> 4: DoEvents: Loop 

'in the search box put cell "A2" value, the word "in" and cell "C1" value 
objIE.document.getElementById("countryCombobox").Value = "GB" 
objIE.document.getElementById("number").Value = ActiveCell.Value 

'click the 'go' button 
objIE.document.getElementById("submit").Click 

'wait again for the browser 
Do While objIE.Busy = True Or objIE.readyState <> 4: DoEvents: Loop 



    Dim vatResponse As String 

    vatResponse = objIE.document.getElementById("vatResponseFormTable").getElementsByTagName("tr")(0).Children(0).textContent 

    ActiveCell.Offset(0, 2).Value = vatResponse 

    ' Selects cell down 1 row from active cell. 

    'Next 
    Application.ScreenUpdating = True  

'close the browser 
objIE.Quit 

ActiveCell.Offset(1, 0).Select 
    'End 

Next 

'exit our SearchBot subroutine 
End Sub 

Also im Grunde auf dieser Code-Zeile klicken:Excel Macro Fehlercode 424 arbeitet langsam, wenn sie durch

vatResponse = objIE.document.getElementById("vatResponseFormTable").getElementsByTagName("tr")(0).Children(0).textContent 

Ich erhalte eine Fehlermeldung, dass ich einen Fehlercode 424

+2

Bitte geben Sie eine bessere Erklärung für Ihr Problem – Jordan

+3

Bitte fügen Sie weitere Informationen und Code zu Ihrer Frage hinzu. Bitte lesen https://StackOverflow.com/Help/how-to-ask – UGP

+0

Ich habe weitere Details hinzugefügt, @UGP –

Antwort

0

Manchmal ist die Seiten werden intern über einige Skripte geladen, so dass das HTML-Element, das Sie tatsächlich erhalten möchten, nicht im Dokument gefunden wird, da der Code sehr schnell ausgeführt wird. Also muss man irgendwie warten bis die Seite komplett geladen ist.

Versuchen Sie diesen Ansatz und sehen Sie, ob der Code ohne Fehler ausgeführt wird.

Sub SearchBot() 

'dimension (declare or set aside memory for) our variables 
Dim objIE As InternetExplorer 'special object variable representing the IE browser 
Dim aEle As HTMLLinkElement 'special object variable for an <a> (link) element 
Dim vatFormTable As IHTMLElement 
Dim tr As IHTMLElement 
Dim y As Integer 'integer variable we'll use as a counter 
Dim result As String 'string variable that will hold our result link 

Dim x As Integer 
Application.ScreenUpdating = False 
' Set numrows = number of rows of data. 
NumRows = Range("A" & Rows.Count).End(xlUp).Row 
' Select cell a1. 
Range("A1").Select 
' Establish "For" loop to loop "numrows" number of times. 
For x = 1 To NumRows 
    ' Insert your code here. 

    'initiating a new instance of Internet Explorer and asigning it to objIE 
Set objIE = New InternetExplorer 

'make IE browser visible (False would allow IE to run in the background) 
objIE.Visible = True 

'navigate IE to this web page (a pretty neat search engine really) 
objIE.navigate "http://ec.europa.eu/taxation_customs/vies/vatResponse.html" 

'wait here a few seconds while the browser is busy 
Do While objIE.Busy = True Or objIE.readyState <> 4: DoEvents: Loop 

'in the search box put cell "A2" value, the word "in" and cell "C1" value 
objIE.document.getElementById("countryCombobox").Value = "GB" 
objIE.document.getElementById("number").Value = ActiveCell.Value 

'click the 'go' button 
objIE.document.getElementById("submit").Click 

'wait again for the browser 
Do While objIE.Busy = True Or objIE.readyState <> 4: DoEvents: Loop 


    On Error Resume Next 
    Dim vatResponse As String 
    Do While vatFormTable Is Nothing 
     Set vatFormTable = objIE.document.getElementById("vatResponseFormTable") 
    Loop 

    Do While tr Is Nothing 
     Set tr = vatFormTable.getElementsByTagName("tr")(0) 
    Loop 

    vatResponse = tr.Children(0).innerText 
    ActiveCell.Offset(0, 2).Value = vatResponse 

    ' Selects cell down 1 row from active cell. 

'Next 
Application.ScreenUpdating = True 

'close the browser 
objIE.Quit 

ActiveCell.Offset(1, 0).Select 
    'End 

Next 

'exit our SearchBot subroutine 
End Sub 
Verwandte Themen