View Full Version : ReadyState WebBrowser Loop.


ugly
02-18-2009, 05:59 AM
I have a WebBrowser Object in a form. I check the ReadyState and when the ReadyState has the value 4 I start processing the text content on the loaded webpage.

The problem is that I do not get out of the loop if I do not active a MsgBox. The MsgBox returns 1,2 or 3 the first time the loop i fired. Next time the MsgBox returns 4 and the loop is ended. If I deactivate the MsgBox I get a never ending loop. I have to use CTRL+Breake and vReadyState is 3. Any ideas?


Private Sub WebBrowser0_Updated(Code As Integer)
Dim Text As String
Dim vReadyState 'check if page is fully loaded. 4 = loaded
vReadyState = 0
Do Until vReadyState = 4
vReadyState = Me!WebBrowser0.ReadyState
'MsgBox vReadyState
Loop


If vReadyState = 4 Then
On Error Resume Next
Text = WebBrowser0.Document.Body.InnerText
Me.Results = Text

lagbolt
02-18-2009, 07:54 AM
If you're trying to determine when a certain page has completed loading, consider something like this...
Private WithEvents m_wb As SHDocVw.InternetExplorer

Private Sub Form_Open(Cancel As Integer)
Set m_wb = Me.WebBrowser0.Object
m_wb.Navigate2 "http://www.yoururl.com"
End Sub

Private Sub m_wb_DocumentComplete(ByVal pDisp As Object, URL As Variant)
If URL = "http://www.yoururl.com" Then
'do something here
'check out pDisp in the locals window
'it contains an object reference to the page in question
End If
End Sub

Or maybe your loop hogs resources and the ready state can't be changed. Maybe you need a DoEvents in your loop?

ByteMyzer
02-18-2009, 08:04 AM
You might try:

Do Until vReadyState = 4
DoEvents
vReadyState = Me!WebBrowser0.ReadyState
Loop

ugly
02-18-2009, 09:29 AM
Thanks alot for the help. The DoEvents made the difference. Now it is working.

Problem Solved. :)