Link to home
Start Free TrialLog in
Avatar of Robert Berke
Robert BerkeFlag for United States of America

asked on

ReadyState get stuck at 3 (READYSTATE_INTERACTIVE)

Please only respond to this question if you have decent experience with internet explorer automation. If you lack that expertise, please wait a few days before chiming in.

I am using vba internetexplorer object to download several sets of data from a website.'

I using Wayt which is an old and well tested subroutine that waits while oie.Busy Or oie.ReadyState <> 4 .

This website seem weird: After pulling the first set, ie.readystate gets stuck at 3 (READYSTATE_INTERACTIVE) which messes up Wayt. As a result Wayt always wait for 20 seconds before exiting
Also, I have often noticed that many of my old programs seem slower that I expected. I think they all might suffer similar problems.

So, I am thinking of changing Wayt to wait while oie.busy or oie.document.readystate <> "complete" or oie.readystate < 3

Or I could add another optional parameter like this, but I am not crazy about that idea.
Has any expert seen similar problems? Do you have any opinions?
Sub wayt(oie, Optional waitseconds As Long = 20, Optional allow3 As Boolean)
 '  Debug.Print "1" & TypeName(oie)       WebBrowser2
  ' Debug.Print "1" & TypeName(oie.Busy)  Boolean
   'Debug.Print "1" & TypeName(oie.ReadyState)  Long
   Dim i As Long
If allow3 Then
    Do While i < waitseconds * 10 And (oie.Busy Or (oie.ReadyState < 3) Or (oie.Document.readysate <> "complete"))
        Sleep 200:
        i = i + 1
    Loop
Else
    Do While i < waitseconds * 10 And (oie.Busy Or (oie.ReadyState <> 4))
        Sleep 200:
        i = i + 1
    Loop
End If

End Sub

Open in new window

ASKER CERTIFIED SOLUTION
Avatar of Daniel Pineault
Daniel Pineault

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
States are in order:

• uninitialized - Has not started loading yet
• loading - Is loading
• loaded - Has been loaded
• interactive - Has loaded enough and the user can interact with it
• complete - Fully loaded

So some scripts can cause this problem. Here you need to look in to the web site itself.

You may carry on after reaching READYSTATE_INTERACTIVE. But this means that not all content is loaded.
Avatar of Robert Berke

ASKER

Thanks
"Doing the process manually works without issue?"

Yes, running manually has no probems. NONETHELESS I can see that the readystate switches from 4 to 3 once the first download begins and it never returns to 4 unless I close the webpage.
In fact, the very first download ends with a 3, so creating a distinct automation does not solve the problem.

Luckily, the following wait code DOES solve my problem.

Dim state As String, ixTries As Long
Const maxTries = 20

For ixTries = 1 To maxTries
    On Error Resume Next
        state = ""
        state = IE.Document.ReadyState
    On Error GoTo err_routine
    Select Case True
        Case IE.Busy, IE.ReadyState < 3, state <> "complete"
        Case Else
            Exit For
    End Select
    DoEvents
    Sleep 100
Next
On Error GoTo 0

Open in new window



But, I've done some more testing and I now agree that only this website has this quirk. All of my other programs MUST check for <>4.  

Also, it turns out that all.document.readystate very occasionally throws error 70 permission denied.  That is why my code had to use on error resume next.   (I dislike doing so, but find it to be the only solution here.)