Home » excel » vba – excel wait for webpage to open

vba – excel wait for webpage to open

Posted by: admin May 14, 2020 Leave a comment


I’ve written some VBA script to load a webpage then copy the entire html contents into a string, then select specific data from that string. In essence I search for a rail timetable, then copy out details for 5 journeys (departure time, interchanges, journey time & cost)

I have the above script sorted to do one search, but I now want to loop it and run approximately 300 searches. The issue I’ve found is that the script won’t wait for the webpage to open, and therefore the string returned is empty, effectively returning nothing.

What I need to do is load an address, wait for the page to load, then continue the script. Do you have any suggestions? I’ve searched a lot and just haven’t been able to sort, I’ve tried Application.Wait in a number of places and still no further ahead.

The code I’m using is below:

Sub CreateIE()
Dim tOLEobject As OLEobject
NRADDRESS = Range("h11")
On Error Resume Next
Set tOLEobject = Worksheets("Sheet1").OLEObjects.Add(ClassType:="Shell.Explorer.2",Link:=False, _
DisplayAsIcon:=False, Left:=0, Top:=15, Width:=912, Height:=345)

For Each tOLEobject In Worksheets("Sheet1").OLEObjects
If tOLEobject.Name = "WebBrowser1" Then
With tOLEobject
.Left = 570
.Top = 1
.Width = 510
.Height = 400
.Name = "WebBrow"
End With

With tOLEobject.Object
.Silent = True
.MenuBar = False
.AddressBar = False
End With
End If
Next tOLEobject


Call ReturnText
End Sub

NRADDRESS is a web address made up of a number of different parameters (origin, destination, date and time)

The “Call ReturnText” is the script I use to copy the website HTML into a string and extract what I want.

How to&Answers:

In that case, you might try something like this:

Set objIE = CreateObject("InternetExplorer.Application")
objIE.navigate strURL

Do While objIE.readyState <> 4 And objIE.Busy

which, I believe, requires a reference to Microsoft Internet Controls.


When I first started using VBA to load webpages, I also used the IE Object, but later found it creates all kinds of complications I didn’t need, when all I really wanted was to download the file. Now I always use URLDownloadToFile.

A good example of it’s use can be found here:

VBA – URLDownloadToFile – Data missing in downloaded file