PDA

View Full Version : Internet Explorer Automation: How to handle connection timeout?



SimpleArts
10-29-2011, 11:00 AM
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!

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

mancubus
10-29-2011, 04:07 PM
hi.

wellcome to VBAX.

perhaps:


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

SimpleArts
10-30-2011, 10:05 AM
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

JP2112
10-31-2011, 09:29 AM
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.


Do Until myTime = 5

' in here, set the value of myTime = Timer - storedTime
Loop


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