Do While ie.Busy Or ie.readyState <> READYSTATE_COMPLETE --- NOT WORKING !!!

srj19

Board Regular
Joined
Aug 23, 2006
Messages
152
I automating a task at work that will save some time. Seems simple enough but for some reason the code I have to cause IE to wait unil ready doesnt seem to work in all cases.

Im using :

'wait until the page loads
Do While ie.Busy Or ie.readyState <> READYSTATE_COMPLETE
DoEvents
Loop

This doesnt seem to work well enough, since actions are occuring that should NOT occur until the browser is ready.

Any other ideas as to how to make IE wait until the page is completely ready? I wondered about possibly checking the .innerHTML to verify that a unique page element is present.


Thanks,

Scott
 

Excel Facts

Whats the difference between CONCAT and CONCATENATE?
The newer CONCAT function can reference a range of cells. =CONCATENATE(A1,A2,A3,A4,A5) becomes =CONCAT(A1:A5)

Norie

Well-known Member
Joined
Apr 28, 2004
Messages
76,286
Office Version
  1. 365
Platform
  1. Windows
Scott

It's kind of hard to help with only 3 lines of code.

Also generally when trying to help with automating IE the URL is a great help.:)(
 

srj19

Board Regular
Joined
Aug 23, 2006
Messages
152
Sorry, I understand the desire to see all the code, I thought the code provided was all the code that pertained to the issue (IE not waiting properly until ready).


I can give this additional code, though the site is password protected and I really can't give that online.
I'm still trying to figure out why it appears that the execution is not waiting until the page loads before clicking the "Edit" button. Strange.
--------------------

Sub a_BATT_automate()

Dim planCode As String
planCode = "8P0YRXCSB"

Dim ie As Object
Set ie = CreateObject("InternetExplorer.Application")


With ie
.Visible = True
.navigate "some site.com"


'wait until the page loads
Do While ie.Busy Or ie.readyState <> READYSTATE_COMPLETE
DoEvents
Loop


'---------------------------selects the scenario
With .document.forms(0)
Set Box = .document.all.Item("ctl00$cphMain$gvSelectScenario$ctl0")
Box.Click
End With


'wait until the page loads
Do While ie.Busy Or ie.readyState <> READYSTATE_COMPLETE
DoEvents
Loop



'---------------------------clicks the edit button
With .document.forms(0)
Set but = .document.all.Item("ctl00$cphMain$btnEdit")
but.Click
End With


'wait until the page loads
Do While ie.Busy Or ie.readyState <> READYSTATE_COMPLETE
DoEvents
Loop


'------------------------ with the Edit Scenario Screen
With .document.forms(0)

'set the Scenario name
Set txtBx = .document.all.Item("ctl00$cphMain$txtScenarioName")
txtBx.Value = planCode & Date

'set the Plan Code
Set txtBx = .document.all.Item("ctl00$cphMain$txtPlanCode")
txtBx.Value = planCode

'set the Plan Name
Set txtBx = .document.all.Item("ctl00$cphMain$txtPlanName")
txtBx.Value = planCode
End With
 

smozgur

BatCoder
Joined
Feb 28, 2002
Messages
1,413
'wait until the page loads
Do While ie.Busy Or ie.readyState <> READYSTATE_COMPLETE
DoEvents
Loop

This doesnt seem to work well enough, since actions are occuring that should NOT occur until the browser is ready.

Any other ideas as to how to make IE wait until the page is completely ready? I wondered about possibly checking the .innerHTML to verify that a unique page element is present.

Hi Scott,

I often find myself in the same trouble and it is usually a pain trying to deal with that failure on ie object. You might end up with a checking objects solutions or even checking the ie's status bar that I have done a few times but I don't find these "patches" very healthy - not very reliable according to me.

However, you migth want to try using IE object itself and use its DocumentComplete event which would be a good solution in most cases. I am not sure if rebuilding your existing code's logic to this one would be efficient but I would give it a try. I hope it helps.

Code:
'Following code goes into a sheet or thisworkbook class object module
Option Explicit
'Requires Microsoft Internet Controls Reference Library
Dim WithEvents ie As InternetExplorer
Sub start_here()
  Set ie = New InternetExplorer
  'Here I wanted to show the progress, so setting ie visible
  ie.Visible = True
  'First URL to go, next actions will be executed in
  'Webbrowser event sub procedure - DocumentComplete
  ie.Navigate "www.google.com"
End Sub
Private Sub ie_DocumentComplete(ByVal pDisp As Object, URL As Variant)
  'pDisp is returned explorer object in this event
  'pDisp.Document is HTMLDocument control that you can use
  'Following is a choice to follow,
  'since there is no do-loop, we have to know where we are by using some reference
  'for example I do check the URL and do the actions according to visited URL
 
  'In this sample, we use google entry page, set search terms, click on search button
  'and navigate to first found URL
  'First condition; after search is made
  'Second condition; search just begins
  If InStr(1, URL, "www.google.com/search?") > 0 Then
    'Open the first returned page
    ie.Navigate pDisp.Document.getelementsbytagname("ol")(0).Children(0).getelementsbytagname("a")(0).href
  ElseIf InStr(1, URL, "www.google.com") > 0 Then
    pDisp.Document.getelementsbyname("q")(0).Value = "VB WebBrowser DocumentComplete Event"
    pDisp.Document.getelementsbyname("btnG")(0).Click
  End If
End Sub
 

srj19

Board Regular
Joined
Aug 23, 2006
Messages
152
Thanks for the tip, I was going crazy trying to find info on documentComplete online and finally gave up. I'm not familiar with this item or events in general, I'm afraid.


I ended up testing for the presence of a unique string in the page html, if not found, clicked the button, wait and test againg. Worked the first time.

Do While InStr(1, ie.document.body.innerHTML, "Edit Scenario", 1) = 0
With .document.forms(0)
Set but = .document.all.Item("ctl00$cphMain$btnEdit")
but.Click
End With

'wait until the page loads
Do While ie.Busy Or ie.readyState <> READYSTATE_COMPLETE
DoEvents
Loop


Loop
 

Watch MrExcel Video

Forum statistics

Threads
1,130,021
Messages
5,639,581
Members
417,100
Latest member
Simon123456789

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Top