How to know if InternetExplorer control has finished loading a multiframe web page
Hi All
I am trying to open a web page using InternetExplorer object in VB6 and I want to set it visible (and extract data from it) only after all its frames are done loading. I have written a function waitFor() to check if the object is still busy. But it is not working well.
I have included the code below.
Can any one please give me any idea about how to solve this problem.
Thanks in Advance.
Code:
' ----------- CODE ----------
Private Function waitFor(ByRef obj As InternetExplorer, Optional duration As Integer = 15) As Integer
' This function should wait for the webpage till it gets loaded or throws error
' then it returns the approprate error code
Dim OK, ERROR, TIMEOUT As Integer
OK = 1
ERROR = 2
TIMEOUT = 3
Dim StartTime As Date
StartTime = Now
' Wait for the webpage while it loads completely or some error occures
label1:
Do While obj.ReadyState <> READYSTATE_COMPLETE And (Now - StartTime) <= duration
DoEvents
Loop
If obj.Busy Then GoTo label1
If (Now - StartTime) >= duration Then ' TIME OUT
waitFor = TIMEOUT
ElseIf obj.Document.title = "ERROR: The requested URL could not be retrieved" Then ' ERROR
waitFor = ERROR
ElseIf obj.Document.title = "" Then ' ERROR, Other conditions may also be added here
waitFor = ERROR
Else
waitFor = OK
End If
End Function
'--------------------
Private Sub Command1_Click()
Dim obj As New InternetExplorer
Dim status as Integer
Dim OK, ERROR, TIMEOUT As Integer
OK = 1
ERROR = 2
TIMEOUT = 3
obj.Navigate2 "http://nseindia.com/"
status = waitFor(obj)
If status = OK Then
obj.Visible = True
' Do something else
Else
MsgBox("Error")
EndIf
End Sub
' -----------END CODE -----
Re: How to know if InternetExplorer control has finished loading a multiframe web pag
Just use a...
Code:
Do While WB.ReadyState <> READYSTATE_COMPLETE
DoEvents
Loop
Good Luck
Re: How to know if InternetExplorer control has finished loading a multiframe web pag
Welcome to the forums Rinku2010. :wave:
When posting your code, please make sure to do so within [CODE] and [/CODE] tags.
Re: How to know if InternetExplorer control has finished loading a multiframe web pag
Hey, Hannes, how did you manage to write out the tags literally? :)
Re: How to know if InternetExplorer control has finished loading a multiframe web pag
Hello my friend! :wave:
You just write &#
then 091; next to it, so it must be on the same line without any spaces :)
That is the shortcut key for the [ sign in HTML
after you have written the HEX code, you just write CODE] exactly after it.
Voila! :)
Just 9 more days and 2 hours to go... then I'm a married man :D :D
Re: How to know if InternetExplorer control has finished loading a multiframe web pag
Quote:
Originally Posted by
HanneSThEGreaT
Welcome to the forums
Rinku2010. :wave:
When posting your code, please make sure to do so within
tags.
Hi Hannes,
Thank you for your reply and for advise about . The code you provided will work only in case of web sites which have one frame. But I am working with a web site with multiple frames. And I want to wait till all my frames are done loading. After that I will do some other processing on the data. During my work, I also have to navigate other web pages, which do have more than one frame and in each case I to wait until all frames are done.
Any suggestion
Re: How to know if InternetExplorer control has finished loading a multiframe web pag
Here's most of it. It also grabs images from the frames it finds (elsewhere)
Code:
' ==============================
Private Declare Function URLDownloadToFile Lib "urlmon" Alias _
"URLDownloadToFileA" (ByVal pCaller As Long, ByVal szURL As String, _
ByVal szFileName As String, ByVal dwReserved As Long, ByVal lpfnCB As _
Long) As Long
Dim colDocuments As Collection
' ==============================
Private Sub Form_Load()
Dim objDoc1 As HTMLDocument
Dim objDoc2 As HTMLDocument
Dim i As Integer
Dim t As Integer
Set objDoc1 = New HTMLDocument
'Create document element from url
Set objDoc2 = objDoc1.createDocumentFromUrl("http://www.hamcams.com", vbNullString)
'Wait till document has loaded
Do While objDoc2.readyState <> "interactive"
DoEvents
Loop
GetFrames
'Loop through images
For t = 1 To colDocuments.Count
For i = 0 To objDoc2.images.length - 1
'download images and save them in app.path
URLDownloadToFile 0, objDoc2.images.Item(i).href, App.Path & "\Images\" & GetFile(objDoc2.images.Item(i).href), 0, 0
Next i
Next t
Set objDoc1 = Nothing
Set objDoc2 = Nothing
Beep
End Sub
Private Sub GetFrames()
'Purpose: searches all frames in the document(s) and adds them to colDocuments
On Error Resume Next
Dim i As Integer, j As Integer
j = 1
Set colDocuments = New Collection
colDocuments.Add WebBrowser1.document
Do While j < colDocuments.Count + 1
For i = 0 To colDocuments.Item(j).frames.length - 1
colDocuments.Add colDocuments.Item(j).frames.Item(i).document
Next i
j = j + 1
Loop
End Sub