Question

Current code is as follows:

Set IE = CreateObject("InternetExplorer.Application")
IE.Navigate "https://www.website.com/"
Application.StatusBar = "https://www.website.com/ is loading. Please wait..."
Do While IE.Busy
    Application.Wait DateAdd("s", 1, Now)
    Loop
Application.StatusBar = "Search form submission. Please wait..."

The issue I am experiencing is that "https://www.website.com/" fails to load from time to time (I believe this is due to me repeatedly accessing it). The result is that the code never moves beyond this loop:

Do While IE.Busy
    Application.Wait DateAdd("s", 1, Now)
    Loop

A logical solution, in my opinion, is to kill IE and restart the whole process after a certain time limit (30 seconds?) has been reached. Please advise as to how to code this solution, and/or, how to code a more effective solution alltogether. Thank you kindly! And I'm using IE10, if that matters at all.

Also, here is a link I found that has somewhat related and relevant information: http://www.ozgrid.com/forum/showthread.php?t=161094

Was it helpful?

Solution

You could try modifying the code to read

Sub LoadWebsite()
Dim iSecondCounter As Integer
Static iAttempts As Integer

iAttempts = iAttempts + 1

Set ie = CreateObject("InternetExplorer.Application")
ie.Navigate "https://www.website.com/"
Application.StatusBar = "https://www.website.com/ is loading. Please wait..."

iSecondCounter = 0
Do While ie.Busy And iSecondCounter < 30
    Application.Wait DateAdd("s", 1, Now)
    iSecondCounter = iSecondCounter + 1
Loop
If iSecondCounter = 30 Then
    ie.Quit
    If iAttempts <= 3 Then Call LoadWebsite
End If

Set ie = Nothing
End Sub

OTHER TIPS

OK, you seem to be asking a new question about getting a collection of HTML elements so I will post it as a new answer. The reason you are getting the object error appears to be because you have not dimensioned objCollection to the correct object. I have modified the code in the origonal answer to do this and set the collection as in your snippet. To use this code I have had to include the reference to Microsoft HTML Object Library.

Please note that this code destroys the element collection before exiting the sub, you will presumably want to do something with it.

Sub LoadWebsite()
Dim iSecondCounter As Integer
Static iAttempts As Integer
Dim objCollection As IHTMLElementCollection
iAttempts = iAttempts + 1

Set ie = CreateObject("InternetExplorer.Application")
ie.Navigate "https://www.website.com/"
Application.StatusBar = "https://www.website.com/ is loading. Please wait..."

iSecondCounter = 0
Do While ie.Busy And iSecondCounter < 30
    Application.Wait DateAdd("s", 1, Now)
    iSecondCounter = iSecondCounter + 1
Loop
If iSecondCounter = 30 Then
    ie.Quit
    If iAttempts <= 3 Then Call LoadWebsite
End If


Set objCollection = (ie.Document.getElementsByTagName("INPUT"))

Cleanup:

Set ie = Nothing
Set objCollection = Nothing

End Sub

Perhaps you can add code to help you wait for it to load and after X seconds you can decide whether or not to wait, or you can gracefully exit / do something else (post a message, etc)

ie.Navigate "http://www.website.com"
Application.Wait Now + TimeSerial(0, 0, 3)

Do While ie.Busy
    DoEvents
Loop
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top