You never check if the loading of a web page is complete!
Your On Error Resume Next statement also hides any errors and prevents you from taking appropriate measures to correct errors by correcting the code. Send a report indicating the error number and the line that causes the error. I have an idea which line and which error, and my answer is for this:
Type 91 error: Set HTMLDOC = .Document
Why?
While you wait, Application.Wait is a really inefficient way to wait for the browser to load, and 2 seconds may not always be enough time. The best way I can think of dealing with this, assuming you get an error like 91, is like this:
How to make a wait loop to control the browser
Basically you need to put the stream in a loop until the browser loads the page. In pseudo code, you do:
Do Until Browser Is Loaded DoEvents 'etc. Loop
Using the WinAPI sleep function
You will need to use the WinAPI Sleep function (many people use DoEvents or Application.Wait , but I found here that Sleep is preferred.
Since this is Declare , you must put it in a regular module; it cannot get into a class module or user form.
'
WinAPI sleep function call
Sleep 250 'sleep for 1/4 of a second, or Sleep 1000 to sleep for a full second, etc.
Putting it all together
You will need to put Do ... Loop , which calls this function immediately after the .Navigate method.
'## This goes in where you do Navigate method: With appIE .Navigate sURL Do Sleep 250 Loop While Not .ReadyState <> 4 'READYSTATE_COMPLETE Set HTMLDoc = .Document End With
If it still does not work ...
Yes, there is always an exception. I do very little internet automation, and most of what I do is just messing around with stuff to help people. I noticed that more and more sites are served dynamically (perhaps AJAX?), And that in these cases the browser may be .ReadyState = READYSTATE_COMPLETE and even .Busy = False , but .Document is still Nothing .
If this happens, you can put a secondary loop, for example:
Dim numberOfAttempts Do While .Document Is Nothing Sleep 250 numberOfAttempts = numberOfAttempts + 1 If numberOfAttempts > 100 Then MsgBox "This is taking too long, try again later." Exit Do End If Loop
You might want to add an iterator / counter, as I did, and simulate a timeout, otherwise this could potentially lead to an infinite loop.