Finding out if instance of IE is busy/ready with InternetExplorer Automation

Posted on 2004-09-22
Last Modified: 2013-11-25
Using VBA, trying to find out if IE has stopped downloading a web page with two frames.

This question is very similar to the question/answer at

in which the .busy and .ReadyState attributes of the WebBrowser control do not work and therefore the busy/readystate status of IE must be monitored with the IE_DownloadComplete event.  I am experiencing the EXACT problem described in Q_20529021.

I have two questions regarding this issue :

1.  On a page with frames, could it be possible that each frame has a .busy or .readystate attribute, something like


and therefore you would have to check the .busy/.readystate attribute of each frame instead of the usual IE.busy ?

2.  If frames do not have their own .busy/.readystate  attributes then the IE_DownloadComplete event must be used to check the status of the IE instance.  

For compatibility reasons I can not use the WebBrowser control.  I can check the status of the IE instance using the IE_DownloadComplete event.  The code for the event is:

  Private Sub IE_DownloadComplete(ByVal pDisp As Object, URL As Variant)
   Debug.Print "Download Is Complete"
  End Sub

but the event is not linked to an object.  How can I link this event to the IE instance given that I am not using a WebBrowserControl?  Are there any other ways to check if the IE instance has stopped processing a web page?

Question by:stefanpantu
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 3

Expert Comment

ID: 12130779
Hi there,

I don't think each frame has it's own readystate. I think the entire webpage is treated as one entity. I had the same problem as you, but changed my code to the following, it works fine for me:

Set oIE = New InternetExplorer
oIE.navigate ("")

Ofcourse if you want to physically see the browser work, you'll have to add oIE.visible = true.

I hope this helps,

Expert Comment

ID: 12130829
Perhaps you could also tell us what you want to do with the IE control... I use it in an application to extract some elements of the HTML for example.


Author Comment

ID: 12140106
If anyone knows how to use the  IE_DownloadComplete outside of the WebBrowser control, I'm sure this is the correct way to solve this problem.  I can't use WebBrowser for compatibility reasons (my app needs to check for the presence of IE 6)

My code and Debug output is below.  In all my experience with  READYSTATE_COMPLETE, it is only different from 4 when a new instance of a browser is first opened.  After a browser is opened READYSTATE_COMPLETE is always 4 (see debug output below).

I was only able to check for specific HTML text after loading this page to make sure a page had completely loaded.  That's clumsy, but not as clumsy as adding a statement like

 Application.Wait (Now + TimeValue("0:00:01"))

It's the only way I was able to get a button click to work to open the final page.  And if the server is slow, then I'm sure this code will break.

Function TestLogonIE() As Boolean
'test to autologon into quote website
 Dim IE As Object
 Dim form0 As Object
 Dim document1 As Object
 Dim hwnd As Long
 Dim oForm As Object
 Dim ieClipboard As Object
 Dim clipboardContents As String
 Static errorStartTime As Date
 On Error GoTo ErrorHandler
 Set IE = New InternetExplorer
 With IE
  .Navigate ""
  .Visible = False 'show/hide browser
  .Silent = True 'suppress dialog boxes
 End With
 Call WaitIE(IE) 'wait to load link, ReadyState seems to be set
                 'to a value other than 4 only when browser is opened
 Set form0 = IE.document.frames(1).document.forms(0)
 Set document1 = IE.document.frames(1).document

 form0.elements("Username").Value = Username
 form0.elements("Password").Value = Password
 Call WaitIEHTML(IE, "Quote")
 Debug.Print "Quote Request Page Loaded, " & "ReadyState: " & CStr(IE.ReadyState)
 Set form0 = IE.document.frames(1).document.forms(0) 'repoint to new webpage
 form0.elements("O_QC").Value = "spx4 c1190" 'goto error 91 if page not loaded
 Application.Wait (Now + TimeValue("0:00:01")) 'don't like Waiting a second here,
                                               'but it's the only way to get the next .Click to work
 Debug.Print "Quote Set, " & "ReadyState: " & CStr(IE.ReadyState)
 document1.images("QC_G").Click 'Click Go button to display quote page
 Call WaitIEHTML(IE, "Key:Real-time QuoteDelayed QuoteInvalid Contract") 'wait for this text
 Debug.Print "Quote Go Button Clicked, " & "ReadyState: " & CStr(IE.ReadyState)
 form0.elements("O_QC").Click 'select the right frame
 Debug.Print "Select the right frame button click, " & "ReadyState: " & CStr(IE.ReadyState)
 Debug.Print "Quote Page Copied To Clipboard," & "ReadyState: " & CStr(IE.ReadyState)
 Set ieClipboard = New DataObject
 clipboardContents = ieClipboard.GetText(1)
 'clipboardContents = document1.Selection.createRange.Text 'this works if text is selected, but no delimitors
 'clipboardContents = document1.body.innertext ' this also works, but again no delimitors!
 Set ieClipboard = Nothing
 Set form0 = Nothing
 Set document1 = Nothing
 Set IE = Nothing
 errorStartTime = #12:00:00 AM#
 Exit Function

 Select Case Err.Number
   Case 91 'Object Variable or With Block Not Set (IE DOM error)
    If errorStartTime = #12:00:00 AM# Then
     errorStartTime = Time
    End If
    If errorStartTime <> #12:00:00 AM# Then
     If Time > TimeSerial(Hour(errorStartTime), Minute(errorStartTime) + 1, Second(errorStartTime)) Then Exit Function
    End If
    Debug.Print "Ran Error Handler"
   Case Else
    Debug.Print CStr(Err.Number) & ": " & Err.Description
 End Select

End Function

Private Sub WaitIE(IE As Object)

  DoEvents 'wait until IE is done loading page.
  Debug.Print "Waiting..."

End Sub

Private Sub WaitIEHTML(IE As Object, HTMLSearchText As String)
'wait for some specific HTML text to be displayed on the page

  DoEvents 'wait until IE is done loading page.
  Debug.Print "Waiting IEHTML..."
 Loop Until CBool(InStr(1, IE.document.frames(1).document.body.innertext, HTMLSearchText))

End Sub



Waiting IEHTML...
Quote Request Page Loaded, ReadyState: 4
Quote Set, ReadyState: 4
Waiting IEHTML...
Waiting IEHTML...
Waiting IEHTML...
Quote Go Button Clicked, ReadyState: 4
Select the right frame button click, ReadyState: 4
Quote Page Copied To Clipboard,ReadyState: 4


Accepted Solution

thunder_moose earned 500 total points
ID: 12140848
Hey there,

Again, I used to have the same problems, but my program is used by a lot of people in the company on a regular basis and it's working flawlessly using READYSTATE_COMPLETE.

Firstly, one thing that I notice in your code, is the IE.Visible = False thing. I used to have that in and it caused a lot of problems. The browser starts up invisible anyway, so you don't need that statement. If you put it in the browser seems to briefly flash in the taskbar.

Secondly: your declarations are a bit vague. It won't make a difference in the code working, but make it a bit easier to see which properties and so on are available to you.

Dim IE As New InternetExplorer
Dim document1 As HTMLDocument

You say that each time you call IE and it finished exploring to a page it remains in the READYSTATE_COMPLETE Readystate? Why not just quit it after each call? IE.Quit?

In my code I have it like this:

Sub NavigateToSite(strWebsite As String)
   'Navigate to default.aspx, wait until page is fully loaded
   Set oIE = New InternetExplorer
   oIE.navigate (strWebsite)
   Do Until oIE.readyState = READYSTATE_COMPLETE
   Set oIEDoc = oIE.Document

   'Check to see if document could be loaded, if not quit program
   If oIEDoc.Title = "Cannot find server" Then
      MsgBox "The Sever is Unreachable"  
   End If
...Code to go through HTML content of file and do what I need
   'Quit IE, release variables
   Set oIE = Nothing
   Set oIEDoc = Nothing
End Sub

I also had the page not loading entirely, also checked for tags in HTML to see if it's done. It used to work 2 out of 3 times and sometimes crash, but like it is now, it works perfectly. I honestly just think that you have something in the wrong place.

LVL 26

Expert Comment

ID: 12141797
I don't quit understand your question. Why you cannot tie up to the event

just declare on the top of your form as

dim WithEvents IE as New InternetExplorer

check with this

Featured Post

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
Hide vba in gp 7 116
VBA Shell can't Find Word document 11 131
how to loop through and process two columns in excel 8 53
Help with Classic ASP - Parameterizing Query 16 24
I’ve seen a number of people looking for examples of how to access web services from VB6.  I’ve been using a test harness I built in VB6 (using many resources I found online) that I use for small projects to work out how to communicate with web serv…
Since upgrading to Office 2013 or higher installing the Smart Indenter addin will fail. This article will explain how to install it so it will work regardless of the Office version installed.
As developers, we are not limited to the functions provided by the VBA language. In addition, we can call the functions that are part of the Windows operating system. These functions are part of the Windows API (Application Programming Interface). U…
Show developers how to use a criteria form to limit the data that appears on an Access report. It is a common requirement that users can specify the criteria for a report at runtime. The easiest way to accomplish this is using a criteria form that a…

726 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question