Thanks Thanks:  0
Likes Likes:  0
Results 1 to 5 of 5

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

  1. #1
    Board Regular
    Join Date
    Aug 2006
    Posts
    152
    Post Thanks / Like
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

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

    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

  2. #2
    Board Regular Norie's Avatar
    Join Date
    Apr 2004
    Location
    Stirling, Scotland
    Posts
    74,626
    Post Thanks / Like
    Mentioned
    44 Post(s)
    Tagged
    4 Thread(s)

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

    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.(
    If posting code please use code tags.

  3. #3
    Board Regular
    Join Date
    Aug 2006
    Posts
    152
    Post Thanks / Like
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

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

    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

  4. #4
    BatCoder
    Join Date
    Feb 2002
    Location
    Turkey
    Posts
    848
    Post Thanks / Like
    Mentioned
    1 Post(s)
    Tagged
    0 Thread(s)

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

    Quote Originally Posted by srj19 View Post
    '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

  5. #5
    Board Regular
    Join Date
    Aug 2006
    Posts
    152
    Post Thanks / Like
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

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

    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

Some videos you may like

User Tag List

Like this thread? Share it with others

Like this thread? Share it with others

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •