• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 4910
  • Last Modified:

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

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
augstef
Asked:
augstef
  • 4
  • 4
1 Solution
 
samopalCommented:
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
 
augstefAuthor Commented:
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
 
samopalCommented:
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
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!

 
augstefAuthor Commented:
Dim WithEvents objIE as object     'is generating the following error

Compile Error:
Expected:identifier

0
 
samopalCommented:
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
 
augstefAuthor Commented:
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
 
samopalCommented:
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
 
augstefAuthor Commented:
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
 
Computer101Commented:
PAQed, with points refunded (75)

Computer101
E-E Admin
0

Featured Post

Receive 1:1 tech help

Solve your biggest tech problems alongside global tech experts with 1:1 help.

  • 4
  • 4
Tackle projects and never again get stuck behind a technical roadblock.
Join Now