ReadyState完全停留在ReadyState = 3上

时间:2018-03-25 00:13:02

标签: vba excel-vba excel

过去几天我一直在运行此代码导航到这个城市数据网页并且它一直运行良好,但突然间我现在围绕readyState = 4获得这个无限循环而且很奇怪事情是有时候它有效,有时它不会。

这是我的代码:

Dim IE as Object
Dim pageaddress as string
Dim city as string
Dim state as string

city = "Chicago"
state = "Illinois"

Set IE = CreateObject("internetExplorer.Application")
IE.Visible = True
pageaddress = "http://www.city-data.com/housing/houses-" & city & "-" & state & ".html"
IE.navigate (pageaddress)
Do
    DoEvents
Loop Until IE.readyState = 4

2 个答案:

答案 0 :(得分:1)

不幸的是,这是网页本身的问题,而不是您的代码。这意味着您将不得不在指定的时间之后添加计时器并刷新页面。

我会记下该页面的平均加载时间,并选择一个远高于该平均值的合理时间。

在这个例子中,我们将使用10秒。

Dim IE As Object
Dim pageaddress As String
Dim city As String
Dim state As String
Dim maxLoadingTime As Single, myTimer As Single

city = "Chicago"
state = "Illinois"

Set IE = CreateObject("internetExplorer.Application")
IE.Visible = True
pageaddress = "http://www.city-data.com/housing/houses-" & city & "-" & state & ".html"
IE.navigate (pageaddress)

'Set the max time to load
maxLoadingTime = 10     '< -- # of seconds to allow page to load -- <
myTimer = Timer

Do
    DoEvents
    If Timer >= maxLoadingTime + myTimer Then
        Debug.Print Time & " Notice: Connection Error. Refreshing webpage"
        IE.stop
        IE.Refresh
    End If
Loop Until IE.readyState = 4

答案 1 :(得分:0)

我使用: 对于t = 1到1000:如果objIE.readyState <> 4或objIE.Busy然后t = 1:DoEvents:下一个t 确保readyState或忙碌状态不会改变。