Consulting

Results 1 to 4 of 4

Thread: Internet Explorer Automation: How to handle connection timeout?

  1. #1

    Internet Explorer Automation: How to handle connection timeout?

    I'm having issues with any and all of my scripts in general that have IE automation included in them. My internet connection will occasionally timeout and stop responding for anywhere from 5 to 60 seconds. This causes my scripts to go into an infinite loop. When this happens the instance of IE will go blank white across the screen as usual and it will just hang there forever until I escape out of it.

    My question is...
    Is there any changes I could make that would allow the script to occasionally try and refresh the IE instance if nothing is happening or perhaps if the elements it's looking for aren't present, or in any way get past a temporarily unresponsive connection.

    Here is my general code for creating the IE instance.
    Any help would be much appreciated!

    [VBA]Set ObjIE = CreateObject("InternetExplorer.Application")
    With ObjIE
    .AddressBar = False
    .StatusBar = False
    .MenuBar = False
    .Toolbar = 0
    .Visible = True
    End With

    sURL = "URL Goes Here"
    ObjIE.navigate sURL

    While ObjIE.Busy
    Wend
    While ObjIE.document.ReadyState <> "complete"
    Wend
    'Buffer time
    Application.Wait Now + TimeValue("00:00:03")

    'Wait time creates buffer.
    'Buffer allows extra time for everything to on page to load
    'because when IE says it's ready, 9 times out of 10, its not.
    'It needs a few extra seconds for elements to become present[/VBA]

  2. #2
    VBAX Guru mancubus's Avatar
    Joined
    Dec 2010
    Location
    "Where I lay my head is home" :D
    Posts
    2,644
    hi.

    wellcome to VBAX.

    perhaps:

    [VBA]
    Do While ObjIE.Busy Or ObjIE.readyState <> READYSTATE_COMPLETE
    DoEvents
    Loop

    [/VBA]
    PLS DO NOT PM; OPEN A THREAD INSTEAD!!!

    1) Posting Code
    [CODE]PasteYourCodeHere[/CODE]
    (or paste your code, select it, click # button)

    2) Uploading File(s)
    Go Advanced / Attachments - Manage Attachments / Add Files / Select Files / Select the file(s) (multiple files can be selected while holding Ctrl key) / Upload Files / Done
    Replace company specific / sensitive / confidential data. Include so many rows and sheets etc in the uploaded workbook to enable the helpers visualize the data and table structure. Helpers do not need the entire workbook.

    3) Testing the Codes
    always back up your files before testing the codes.

    4) Marking the Thread as Solved
    from Thread Tools (on the top right corner, above the first message)

  3. #3

    More specific

    I guess maybe one direction to look in is the answer to this question...

    What is the technical difference between the ie.busy loop and the readystate complete loop. What conditions would are each of them looking for.

    Because one thing i'm considering is letting it look for one or the other instead of both, to keep it more simple and get to goto the next line of code which will then analyze the url bar in IE for a match to the original address, which of course it won't have on an unsuccessful attempt and then if it doesn't find it, do a simple ie.refresh command... which interestingly enough I can do that while the script is running and it will continue on through. The problem with that though, is that it requires babysitting the script while it runs in order to keep it running

  4. #4
    VBAX Expert JP2112's Avatar
    Joined
    Oct 2008
    Location
    Astoria, NY
    Posts
    590
    Location
    In addition to what mancubus suggested, how about a variable to hold the current time?

    For example, before you enter the Do loop, store the current time ('Timer' returns the number of seconds since midnight). Inside the loop, check if the current Timer value minus the stored value is greater than a predetermined amount of time you want to wait.

    Or even better, make it the condition of the Do Loop, i.e.

    [VBA]
    Do Until myTime = 5

    ' in here, set the value of myTime = Timer - storedTime
    Loop
    [/VBA]

    Then you can refresh or display a message box, or whatever you like.
    Regards,
    JP

    Read the FAQ
    Getting free help on the web
    My website
    Please use [vba][/vba] tags when posting code

Posting Permissions

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