Sorry, I accidently hit send.
Hi,
I playing with IE automation. Many scripts in this newsgroup show code like
the following to open a web page and wait for its completion:
Set oIEApp = CreateObject("InternetExplorer.Application")
With oIEApp
.Visible = True
.Resizable = True
.FullScreen = False
.Height = 600 : .Width = 600
.Top = 50 : .Left = 100
.Navigate("msdn.microsoft.com")
Do Until .ReadyState = 4 : WScript.Sleep 100 : Loop
MsgBox "Done navigating to msdn.microsoft.com"
End With
Other scripts listen for the DocumentComplete event. If I count the
occurences of the DocumentComplete event, I find that my 'done' message box
appears after the DocumentComplete event has fired twice, and that the
DocumentComplete event fires once more, perhaps right when
msdn.microsoft.com is truely done loading. When just starting IE and
navigating to one URL, waiting for a ReadyState of 4 is sufficient.
Many of us are not satisfied with navigating to only one URL; we want to go
more places. But the ReadyState remains 4 for a hundred or more millisecons
after specifying a new URL, then changes to 3, and finally changes back to 4
when the page has loaded. So we need logic like this for the second URL:
Option Explicit
Dim oIEApp
Set oIEApp = CreateObject("InternetExplorer.Application")
With oIEApp
.Visible = True
.Resizable = True
.FullScreen = False
.Height = 600 : .Width = 600
.Top = 50 : .Left = 100
.Navigate("msdn.microsoft.com")
Do Until .ReadyState = 4 : WScript.Sleep 100 : Loop
MsgBox "Done navigating to msdn.microsoft.com"
.Navigate("aol.com")
Do While.ReadyState = 4 : WScript.Sleep 2 : Loop
Do Until .ReadyState = 4 : WScript.Sleep 100 : Loop
MsgBox "Done navigating to aol.com"
End With
With this script, most of the time the AOL done message occurs when the page
really is loaded. Occasionally, the AOL done message occurs long before the
page really is loaded, especially when CPU usage is high.
From this I have concluded that neither ReadyState nor the DocumentComplete
event are reliable indicators of a web page's being complete.
I've monitored how often various events occur by the time the MsgBoxes
appear, and I'm currently using the following logic to determine completion.
Option Explicit
Dim oIEApp
Dim iCount_BeforeNavigate2, iCount_NavigateComplete2
Set oIEApp = WScript.CreateObject("InternetExplorer.Application", "event_")
With oIEApp
.Visible = True
.Resizable = True
.FullScreen = False
.Height = 600 : .Width = 600
.Top = 50 : .Left = 100
.Navigate("msdn.microsoft.com")
Do Until .ReadyState = 4 : WScript.Sleep 100 : Loop
Do Until iCount_BeforeNavigate2 = iCount_NavigateComplete2
WScript.Sleep 100
Loop
MsgBox "Done navigating to msdn.microsoft.com"
.Navigate("aol.com")
Do While.ReadyState = 4 : WScript.Sleep 2 : Loop
Do Until .ReadyState = 4 : WScript.Sleep 100 : Loop
Do Until iCount_BeforeNavigate2 = iCount_NavigateComplete2
WScript.Sleep 100
Loop
MsgBox "Done navigating to aol.com"
End With
Sub event_BeforeNavigate2(objBrowser, strURL, Flags, _
TargetFrameName, PostData, Headers, bCancel)
iCount_BeforeNavigate2 = iCount_BeforeNavigate2 + 1
End Sub
sub event_NavigateComplete2(objBrowser, strURL)
iCount_NavigateComplete2 = iCount_NavigateComplete2 + 1
End Sub
This seems like overkill. Does anyone have a simpler way to reliably detect
document complete?
Thanks,
-Paul Randall