vba excel等待网页打开
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/23631621/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
excel wait for webpage to open
提问by user3533028
I've written some VBA script to load a webpage then copy the entire html contents into a string, then select specific data from that string. In essence I search for a rail timetable, then copy out details for 5 journeys (departure time, interchanges, journey time & cost)
我编写了一些 VBA 脚本来加载网页,然后将整个 html 内容复制到一个字符串中,然后从该字符串中选择特定数据。本质上,我会搜索铁路时刻表,然后复制 5 趟旅程的详细信息(出发时间、换乘站、旅程时间和费用)
I have the above script sorted to do one search, but I now want to loop it and run approximately 300 searches. The issue I've found is that the script won't wait for the webpage to open, and therefore the string returned is empty, effectively returning nothing.
我对上面的脚本进行了排序以进行一次搜索,但我现在想循环它并运行大约 300 次搜索。我发现的问题是脚本不会等待网页打开,因此返回的字符串为空,实际上什么都不返回。
What I need to do is load an address, wait for the page to load, then continue the script. Do you have any suggestions? I've searched a lot and just haven't been able to sort, I've tried Application.Wait
in a number of places and still no further ahead.
我需要做的是加载一个地址,等待页面加载,然后继续脚本。你有什么建议吗?我已经搜索了很多,只是无法排序,我已经Application.Wait
在很多地方尝试过,但仍然没有进一步。
The code I'm using is below:
我正在使用的代码如下:
Sub CreateIE()
Dim tOLEobject As OLEobject
Dim NRADDRESS As String
NRADDRESS = Range("h11")
On Error Resume Next
Worksheets("Sheet1").Shapes.Range(Array("WebBrow")).Delete
Set tOLEobject = Worksheets("Sheet1").OLEObjects.Add(ClassType:="Shell.Explorer.2",Link:=False, _
DisplayAsIcon:=False, Left:=0, Top:=15, Width:=912, Height:=345)
For Each tOLEobject In Worksheets("Sheet1").OLEObjects
If tOLEobject.Name = "WebBrowser1" Then
With tOLEobject
.Left = 570
.Top = 1
.Width = 510
.Height = 400
.Name = "WebBrow"
End With
With tOLEobject.Object
.Silent = True
.MenuBar = False
.AddressBar = False
.Navigate NRADDRESS
End With
End If
Next tOLEobject
Sheets("Sheet2").Activate
Sheets("Sheet1").Activate
Call ReturnText
End Sub
NRADDRESS
is a web address made up of a number of different parameters (origin, destination, date and time)
NRADDRESS
是由许多不同参数(来源、目的地、日期和时间)组成的网址
The "Call ReturnText" is the script I use to copy the website HTML into a string and extract what I want.
“Call ReturnText”是我用来将网站 HTML 复制到字符串中并提取我想要的内容的脚本。
回答by Michael Blaustein
In that case, you might try something like this:
在这种情况下,您可以尝试以下操作:
Set objIE = CreateObject("InternetExplorer.Application")
objIE.navigate strURL
Do While objIE.readyState <> 4 And objIE.Busy
DoEvents
Loop
which, I believe, requires a reference to Microsoft Internet Controls.
我相信,这需要参考 Microsoft Internet Controls。
回答by Michael Blaustein
When I first started using VBA to load webpages, I also used the IE Object, but later found it creates all kinds of complications I didn't need, when all I really wanted was to download the file. Now I always use URLDownloadToFile.
当我第一次开始使用 VBA 加载网页时,我也使用了 IE 对象,但后来发现它产生了我不需要的各种复杂情况,而我真正想要的是下载文件。现在我总是使用 URLDownloadToFile。
A good example of it's use can be found here:
可以在此处找到使用它的一个很好的示例: