Solved

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

Posted on 2004-08-08
10
4,616 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
[X]
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
  • 4
  • 4
10 Comments
 
LVL 9

Expert Comment

by:samopal
ID: 11755677
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
ID: 11757410
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
ID: 11757971
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
Get Database Help Now w/ Support & Database Audit

Keeping your database environment tuned, optimized and high-performance is key to achieving business goals. If your database goes down, so does your business. Percona experts have a long history of helping enterprises ensure their databases are running smoothly.

 

Author Comment

by:augstef
ID: 11759104
Dim WithEvents objIE as object     'is generating the following error

Compile Error:
Expected:identifier

0
 
LVL 9

Expert Comment

by:samopal
ID: 11764796
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
ID: 11769019
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
ID: 11775349
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
ID: 11801100
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
ID: 12059827
PAQed, with points refunded (75)

Computer101
E-E Admin
0

Featured Post

Independent Software Vendors: 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

When table data gets too large to manage or queries take too long to execute the solution is often to buy bigger hardware or assign more CPUs and memory resources to the machine to solve the problem. However, the best, cheapest and most effective so…
A company’s centralized system that manages user data, security, and distributed resources is often a focus of criminal attention. Active Directory (AD) is no exception. In truth, it’s even more likely to be targeted due to the number of companies …
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…

759 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