Improve company productivity with a Business Account.Sign Up

x
?
Solved

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

Posted on 2004-08-08
10
Medium Priority
?
5,059 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
9 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
Build your data science skills into a career

Are you ready to take your data science career to the next step, or break into data science? With Springboard’s Data Science Career Track, you’ll master data science topics, have personalized career guidance, weekly calls with a data science expert, and a job guarantee.

 

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

Get 10% Off Your First Squarespace Website

Ready to showcase your work, publish content or promote your business online? With Squarespace’s award-winning templates and 24/7 customer service, getting started is simple. Head to Squarespace.com and use offer code ‘EXPERTS’ to get 10% off your first purchase.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

In this blog post, we’ll look at how using thread_statistics can cause high memory usage.
A method of moving multiple mailboxes (in bulk) to another database in an Exchange 2010/2013/2016 environment...
In this video, Percona Solution Engineer Dimitri Vanoverbeke discusses why you want to use at least three nodes in a database cluster. To discuss how Percona Consulting can help with your design and architecture needs for your database and infras…
SQL Database Recovery Software repairs the MDF & NDF Files, corrupted due to hardware related issues or software related errors. Provides preview of recovered database objects and allows saving in either MSSQL, CSV, HTML or XLS format. Ensures recov…

606 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