vbaexcelnavigateurl

VBA: Attempting to access website, but website occasionally fails to load


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


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