简体   繁体   中英

Excel VBA Error 462 With Internet Explorer

I have this function:

Sub CreateDatabase()
    Dim ieNewPage As InternetExplorer, TableRows As Object, TableRow As Object
    Dim TableRowsSpecificOwner As Object, TableRowSpecificOwner As Object, TagNeeded As Object
    Set TableRows = GetData()
    Set ieNewPage = New InternetExplorer
    For Each TableRow In TableRows
        ieNewPage.navigate CStr(TableRow.href)
        Do While ieNewPage.ReadyState <> 4
            DoEvents
        Loop
        ieNewPage.Visible = True
        Set TableRowsSpecificOwner = ieNewPage.document.querySelectorAll("tr[class='puce_texte']")
        For Each TableRowSpecificOwner In TableRowsSpecificOwner
            If Excel.Application.IfError(Excel.Application.Search("France", TableRowSpecificOwner.innerText), -1) > 0 Then
                    Debug.Print TableRow.innerText
                    Debug.Print TableRowSpecificOwner.getElementsByTagName("a")(1).innerText
            End If
        Next
        Excel.Application.Wait (Now + TimeValue("0:00:02"))
    Next
End Sub

What it should to do is loop through the hrefs I get with the function GetData() , navigate the page, get some data and move to the next one.

This works for the first link but then I receive the error 462:

The remote server does not exists or is unavailable.

The error is on this line:

ieNewPage.navigate CStr(TableRow.href)

How the heck do I solve this?

There's an article about this error: Excel automation fails second time code runs , you could check it. The cause of the error is:

Visual Basic has established a reference to Excel because of a line of code that calls an Excel object, method, or property without qualifying the element with an Excel object variable. Visual Basic does not release this reference until you end the program. This errant reference interferes with automation code when the code is run more than one time.

I think the error occurs because you didn't call the Set of ieNewPage for the second time. You could try to put Set ieNewPage = New InternetExplorer in the loop like this:

...    
Set TableRows = GetData()    
For Each TableRow In TableRows
    Set ieNewPage = New InternetExplorer
    ieNewPage.navigate CStr(TableRow.href)
    ...
Next
...

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM