ptownbro
ptownbro

Reputation: 1292

Best Way to Check if Browser Page is Ready in VBA

I've seen three different ways to check if the page I'm navigating to is ready. As shown in the sample code below.

It seems to me Method 1 is the best, but hoping an expert out there can tell otherwise or even better... provide the right way to do it if there is something different.

Here's the sample code

Sub OpenBrowser()
    Dim vOBJBROWSER As Object
    Set vOBJBROWSER = CreateObject("InternetExplorer.Application")

    vOBJBROWSER.Navigate "http://stackoverflow.com"

    'Method 1
    Do While vOBJBROWSER.Busy Or vOBJBROWSER.ReadyState <> 4
        DoEvents
    Loop

    'Method 2
    Do While vOBJBROWSER.ReadyState < 4
        DoEvents
    Loop
    
    'Method 3
    Do
    Loop Until vOBJBROWSER.ReadyState = READYSTATE_COMPLETE

    vOBJBROWSER.Visible = True
End Sub

Upvotes: 0

Views: 1979

Answers (1)

Peyter
Peyter

Reputation: 484

The IE browser is going to make you really hate life in the long run.

Just like any browser'ed solution in webs scraping, you only need the browser, if you cant figure out what the resource is you're trying to load.

Consider all the over-head, javascript, CSS, potential tracking cookies, that accompany using a browser.

Now if you know what you want, and see in Chrome Dev Tools how it loads - you can use VBA's HTTP request libraries and you'll have a much better time.

The pro to using a HTTP request is that even it's a stream or chunked, you can control and easily measure when the message is done. A web page you'll always be stuck trying to figure out what the status code is, and sub frames, and all kinds of crap.

Highly recommend, channeling the frustration of IE automation into a learning experience with HTTP and chrome dev tools. You will 100% be less likely to smash your keyboard.

Upvotes: 2

Related Questions