Skip to content Skip to sidebar Skip to footer

Vba: How To Wait For Page To Load

I'm in the middle of writing a script for signing into a form, and filling in a bunch of 'sign up' information and clicking submit. Thus far, i have managed to get the script to na

Solution 1:

I much prefer adding a timed loop testing for presence of an element as this allows you to Exit earlier is element found and is thus potentially more efficient than a hard coded sleep time which will always be completed.

Dim t As Date, ele AsObjectConst MAX_WAIT_SEC As Long = 10'<==Adjust wait time

While ie.Busy Or ie.readyState < 4: DoEvents: Wend
t = timer
Do 
    DoEvents
    On Error Resume Next
    Set ele = IE.document.getElementByID("firstname")
    If Timer - t > MAX_WAIT_SEC Then Exit Do
    On Error GoTo 0
Loop While ele Is Nothing

If Not ele Is Nothing Then
    'do something 
End If

Solution 2:

If IE.ReadyState and IE.Busy aren't working, you could just test for an element you know will exist after the page has loaded and add the Sleep API so you aren't rapid-fire testing Fname until it's not nothing.

Sleep API is

DeclareSub Sleep Lib"kernel32" (ByVal dwMilliseconds AsLong)

Then change your While-Wend to

DoUntilNot Fname IsNothingSet Fname = IE.document.getElementByID("firstname")
    Sleep 1000'<- make this however long you want it to wait in msLoop

Post a Comment for "Vba: How To Wait For Page To Load"