Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

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

Posted on 2004-08-08
10
Medium Priority
?
4,770 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
Prepare for your VMware VCP6-DCV exam.

Josh Coen and Jason Langer have prepared the latest edition of VCP study guide. Both authors have been working in the IT field for more than a decade, and both hold VMware certifications. This 163-page guide covers all 10 of the exam blueprint sections.

 

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

What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

Question has a verified solution.

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

In this blog post, we’ll look at how using thread_statistics can cause high memory usage.
In this article, I’ll look at how you can use a backup to start a secondary instance for MongoDB.
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…
In this video, Percona Director of Solution Engineering Jon Tobin discusses the function and features of Percona Server for MongoDB. How Percona can help Percona can help you determine if Percona Server for MongoDB is the right solution for …

715 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