Home » excel » excel – Perform operations in different IE tabs

excel – Perform operations in different IE tabs

Posted by: admin May 14, 2020 Leave a comment

Questions:

I would like to search for city in one IE tab then open my generated link in another tab. If I run these operations separately everything work however I am not able to make it work all together. What is possibly wrong?

Debugger points to .Document.getElementsByName("q")(0).Value = "Dublin" saying

Run-time error ‘-2147467259 (80004005)’: Method ‘Document’ of object
‘IWebBrowser 2’ failed

Here is my code:

Option Explicit
Sub GetFlightRates()
Dim appIE As Object
Set appIE = CreateObject("internetexplorer.application")

With appIE
    .Navigate "https://maps.google.com"
    .Document.getElementsByName("q")(0).Value = "Dublin"
    .Document.getElementsByClassName("searchbox-searchbutton")(0).Click

    .Navigate "https://www.google.co.in/flights/", CLng(2048) '2nd
    .Visible = True
End With

Do While appIE.Busy
    DoEvents
Loop

'appIE.Quit
Set appIE = Nothing

End Sub
How to&Answers:

You need a page load wait before attempting to access .document

Option Explicit
Sub OpenTabs()
    Dim appIE As Object
    Set appIE = CreateObject("internetexplorer.application")

    With appIE
        .Visible = True
        .navigate "https://maps.google.com"
        While .Busy Or .readyState < 4: DoEvents: Wend
        .document.getElementsByName("q")(0).Value = "Dublin"
        .document.getElementsByClassName("searchbox-searchbutton")(0).Click

        .navigate "https://www.google.co.in/flights/", CLng(2048) '2nd
        While .Busy Or .readyState < 4: DoEvents: Wend
    End With

    Stop
    'appIE.Quit
    Set appIE = Nothing
End Sub