1

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?

8
  • (TableRows contains <a> tags with a href inside and a textpart I also need) Commented Nov 22, 2019 at 14:13
  • It may be useful to know, what kind of object you receive after running the function `GetData()' or the function code itself. Commented Nov 22, 2019 at 14:15
  • @ZygD an example of a object in TableRows is something like this: <a class="classname" href="link I need">Text info I also need </a> Commented Nov 22, 2019 at 14:20
  • Run typename(TableRows) and tell us what type name do you get. Commented Nov 22, 2019 at 14:26
  • @ZygD I get: DispHTMLElementCollection Commented Nov 22, 2019 at 14:29

1 Answer 1

1

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
...
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.