我想在一个IE标签中搜索城市,然后在另一个标签中打开生成的链接。如果我分别运行这些操作,那么一切都会正常工作,但是我无法使其全部正常工作。可能是什么问题?
调试器指向.Document.getElementsByName("q")(0).Value = "Dublin"
运行时错误'-2147467259(80004005)':对象的方法'文档' “ IWebBrowser 2”失败
这是我的代码:
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
答案 0 :(得分:2)
您需要先等待页面加载,然后才能尝试访问.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