Solved

VBA and Internet Explorer - Wait for web page to download complete

Posted on 2004-08-08
10
4,369 Views
Last Modified: 2012-06-27
I try to drive the Internet Explorer via VBA. The code is working but on occassions the the web site may not respond or the netwok is slow. In my code I'm navigating on different web pages, first I loggin, after I'm moving on other page,I'm searching for an order# (it's running a web query and the wait time varies), after I get the query result I have to select a check box and finally I click a button to send an email confirmation. Sometimes the code executes before the query is finisshed just because the ReadyState shows complete. I used few codes to check for the ready state but sometimes is working sometimes no. These are the scenarious that I used

Do Until objIE.ReadyState = READYSTATE_COMPLETE
DoEvents
Loop
-----------------------------
Dim StartTime as Date

StartTime = Now

Do Until objIE.ReadyState = READYSTATE_COMPLETE Or
(Now-StartTime)>TimeValue("00:00:15")

DoEvent
Loop
---------------------------------

sub DownloadWeb()

do while objIE.Busy
doevents
Loop

do while objIE.ReadyState <> READYSTATE_COMPLETE
doevents
Loop
end sub

-----------------------------

ReadyState = READYSTATE_COMPLETE is not the most reliable way to check for the web page actually being completely downloaded.  There are many sites that will generate ReadyState complete (particularly on a frame), and then proceed to continue to load (generally in other frames).

Can some one please help on this issue?
0
Comment
Question by:augstef
  • 4
  • 4
10 Comments
 
LVL 9

Expert Comment

by:samopal
Comment Utility
You should use DocumentComplete event of IE Control :


Dim WithEvents objIE as object

Set objIE=CreateObject("InternetExplorer.Application")
objIE.Visible=True
objIE.Navigate "....."

Private Sub objIE_DocumentComplete(ByVal pDisp As Object,
                                         URL As Variant)
   If (pDisp Is objIE.Object) Then
      Debug.Print "Web document is finished downloading"
   End If
End Sub

Hope This helps,
D'Al
0
 

Author Comment

by:augstef
Comment Utility
Can you please give more details...how to use it?

Dim WithEvents objIE as object   'this has to be in a class module?

what about the rest, the Private Sub and
Set objIE=CreateObject("InternetExplorer.Application")
objIE.Visible=True
objIE.Navigate "....."
0
 
LVL 9

Expert Comment

by:samopal
Comment Utility
Create a new form, add cmdNavigate button. Add this code to form code :

Dim WithEvents objIE as object

Private Sub Form_Load()
   Set objIE=CreateObject("InternetExplorer.Application")
End Sub

Private Sub cmdNavigate_Click()
  objIE.Visible=True
  objIE.Navigate "www.microsoft.com"
End Sub

Private Sub objIE_DocumentComplete(ByVal pDisp As Object,
                                         URL As Variant)
   If (pDisp Is objIE.Object) Then
      Debug.Print "Web document is finished downloading"
   End If
End Sub
0
 

Author Comment

by:augstef
Comment Utility
Dim WithEvents objIE as object     'is generating the following error

Compile Error:
Expected:identifier

0
IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

 
LVL 9

Expert Comment

by:samopal
Comment Utility
Add reference to Microsoft Internet Control (shdocvw.dll)

Dim WithEvents objIE As InternetExplorer

Private Sub Form_Load()
   Set objIE = CreateObject("InternetExplorer.Application")
End Sub

Private Sub cmdNavigate_Click()
  objIE.Visible = True
  objIE.Navigate "www.microsoft.com"
End Sub

Private Sub objIE_DocumentComplete(ByVal pDisp As Object, URL As Variant)
   If (pDisp Is objIE) Then
      Debug.Print "Web document is finished downloading"
   End If
End Sub

D'Al
0
 

Author Comment

by:augstef
Comment Utility
it's working now, but still I do not know how to use it in the context of my script. Let's give you more details...first step...I navigate to a home page, let's say www.msn.com...after the page is downloaded complete my script has to move to an other page, let's say www.hotmail.com (in my script not these are the web pages and there are more than two  but it's something similar)....in my script I'm using send keys to navigate to the next page, sometimes is working sometimes no depending by network connection.What I want is to sendkeys just when the page is downloaded.

thx
0
 
LVL 9

Expert Comment

by:samopal
Comment Utility
There are several ways to achive this. The simplest will be :

Dim WithEvents objIE As InternetExplorer
Dim DownloadComplete as boolean

Private Sub Form_Load()
   Set objIE = CreateObject("InternetExplorer.Application")
End Sub

Private Sub cmdNavigate_Click()
  objIE.Visible = True
  DownloadComplete = False
  objIE.Navigate "www.microsoft.com"
  Do while NOT DownloadComplete
    DoEvents
  Loop
 
  DownloadComplete = False
  objIE.Navigate "www.nextpage.com"
  Do while NOT DownloadComplete
    DoEvents
  Loop
 
End Sub

Private Sub objIE_DocumentComplete(ByVal pDisp As Object, URL As Variant)
   If (pDisp Is objIE) Then
      DownloadComplete = True
   End If
End Sub
0
 

Author Comment

by:augstef
Comment Utility
still doesn't work properlly; if the connection is slow is not waiting the next page to be downloaded complete and it is firing the next event. Have to mention that the next page(s) contains frames.
0
 
LVL 1

Accepted Solution

by:
Computer101 earned 0 total points
Comment Utility
PAQed, with points refunded (75)

Computer101
E-E Admin
0

Featured Post

Enabling OSINT in Activity Based Intelligence

Activity based intelligence (ABI) requires access to all available sources of data. Recorded Future allows analysts to observe structured data on the open, deep, and dark web.

Join & Write a Comment

In today’s complex data management environments, it is not unusual for UNIX servers to be dedicated to a particular department, purpose, or database.  As a result, a SAS® data analyst often works with multiple servers, each with its own data storage…
SQL Command Tool comes with APEX under SQL Workshop. It helps us to make changes on the database directly using a graphical user interface. This helps us writing any SQL/ PLSQL queries and execute it on the database and we can create any database ob…
Video by: Steve
Using examples as well as descriptions, step through each of the common simple join types, explaining differences in syntax, differences in expected outputs and showing how the queries run along with the actual outputs based upon a simple set of dem…
Polish reports in Access so they look terrific. Take yourself to another level. Equations, Back Color, Alternate Back Color. Write easy VBA Code. Tighten space to use less pages. Launch report from a menu, considering criteria only when it is filled…

743 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

Need Help in Real-Time?

Connect with top rated Experts

16 Experts available now in Live!

Get 1:1 Help Now