我试图做一些网页在Excel VBA中拼抢。 这是我遇到的麻烦的部分代码:
IE.Navigate URL
Do
DoEvents
Loop While IE.ReadyState <> 4 Or IE.Busy = True
Set doc = IE.document
运行此之后doc
包含仍然在其未执行使用JavasScript HTML。 这是一个还没有被执行脚本的签名:
<SCRIPT type=text/javascript>
goosSearchPage.Initialize(...)...;
</SCRIPT>
我可以等待做执行Application.Wait(Now + TimeValue(x))
但是这确实是不尽如人意,随着时间的脚本需要执行量取决于输入变化很大。
有没有办法要么等待脚本来完成评估或干脆直接在评估脚本doc
对象?
我发现代码,并等待一个页面即可完成。 每个音符在这里 ,它需要的Microsoft Internet控制在你的代码的参考。
代码复制在这里,以防万一死链接:
'Following code goes into a sheet or thisworkbook class object module
Option Explicit
'Requires Microsoft Internet Controls Reference Library
Dim WithEvents ie As InternetExplorer
Sub start_here()
Set ie = New InternetExplorer
'Here I wanted to show the progress, so setting ie visible
ie.Visible = True
'First URL to go, next actions will be executed in
'Webbrowser event sub procedure - DocumentComplete
ie.Navigate "www.google.com"
End Sub
Private Sub ie_DocumentComplete(ByVal pDisp As Object, URL As Variant)
'pDisp is returned explorer object in this event
'pDisp.Document is HTMLDocument control that you can use
'Following is a choice to follow,
'since there is no do-loop, we have to know where we are by using some reference
'for example I do check the URL and do the actions according to visited URL
'In this sample, we use google entry page, set search terms, click on search button
'and navigate to first found URL
'First condition; after search is made
'Second condition; search just begins
If InStr(1, URL, "www.google.com/search?") > 0 Then
'Open the first returned page
ie.Navigate pDisp.Document.getelementsbytagname("ol")(0).Children(0).getelementsbytagname("a")(0).href
ElseIf InStr(1, URL, "www.google.com") > 0 Then
pDisp.Document.getelementsbyname("q")(0).Value = "VB WebBrowser DocumentComplete Event"
pDisp.Document.getelementsbyname("btnG")(0).Click
End If
End Sub
实际上,你可以评估与IE窗口的JavaScript功能。 但你得建立一个回调,因为函数将被评估异步。