Waiting on Websites

On this post, Jan Karel Pieterse commented that I should put a timer in my wait code and I couldn’t agree more. It seemed like a good opportunity to just get that code out of the main procedure. Here’s what I came up with.

Sub Gotosite()
   
    Dim ieApp As InternetExplorer
   
    On Error GoTo ErrHandler
   
    Set ieApp = New InternetExplorer
   
    ieApp.Visible = True
   
    ieApp.Navigate “http://severe-frost-552.heroku.com/”
    If Not IEReady(ieApp) Then Err.Raise 9999, , “Website not available”
   
ErrExit:
    ieApp.Quit
    Exit Sub
   
ErrHandler:
    MsgBox Err.Description
    Resume ErrExit
   
End Sub

Private Function IEReady(ByRef ieApp As InternetExplorer) As Boolean
   
    Dim dtStart As Date
    Dim bReturn As Boolean
    Dim dtLimit As Date
   
    bReturn = True
    dtStart = Now
    dtLimit = TimeValue(“00:00:10”)
   
    Do
        DoEvents
        If Now – dtStart > dtLimit Then
            bReturn = False
            Exit Do
        End If
    Loop Until Not ieApp.Busy And ieApp.ReadyState = READYSTATE_COMPLETE
   
    IEReady = bReturn
   
End Function

Before you automate Internet Explorer, be sure to read JP’s comment. It seems superior to my method and I shall be implementing it forthwith.

Posted in Uncategorized


Posting code? Use <pre> tags for VBA and <code> tags for inline.

Leave a Reply

Your email address will not be published.