Last active
November 4, 2015 17:14
-
-
Save LudovicAL/5646dc37367f4d27e45b to your computer and use it in GitHub Desktop.
VBA function detecting, in an active Internet Explorer instance, the completion of a web page loading, including its frames if any
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
'When you call this function, make sure that you have a SHDocVw.InternetExplorer object that's already initialisez (it must refer to an active IE instance). The HTMLDocument object may have a null value. | |
Function loading(pageIE As SHDocVw.InternetExplorer, ByRef HTMLdoc As HTMLDocument) As Boolean | |
'Chronometer declaration and initialization | |
Dim chrono1 As Date | |
Dim chrono2 As Date | |
chrono1 = TimeValue(Now) | |
chrono2 = TimeValue(Now) + TimeValue("00:00:10") //Specify here the amount of time the function should wait for the page to load before it throws an error to the user. | |
'Waiting for the SHDocVw.InternetExplorer | |
Do While (chrono1 < chrono2 And pageIE.readyState <> READYSTATE_COMPLETE And pageIE.Busy <> False) | |
DoEvents | |
chrono1 = TimeValue(Now) | |
Loop | |
'Waiting for the HTMLDocument | |
Set HTMLdoc = pageIE.document | |
Do While (chrono1 < chrono2 And HTMLdoc.readyState <> "complete") | |
DoEvents | |
chrono1 = TimeValue(Now) | |
Set HTMLdoc = pageIE.document | |
Loop | |
'Waiting for the frames (if any) | |
loadingFrame HTMLdoc, chrono1, chrono2 | |
'If everything went well, the function should return true and the HTMLDocument should be initialized and ready to use | |
If (chrono1 < chrono2) Then | |
loading = True | |
Else | |
MsgBox "Error while loading the web page." | |
loading = False | |
End If | |
End Function | |
'The following sub is called by the function "loading" (the previous function) when a frame is detected in a web page. | |
Sub loadingFrame(HTMLdoc As HTMLDocument, chrono1 As Date, chrono2 As Date) | |
'Variables declaration | |
Dim myFrame As HTMLIFrame | |
Dim HTMLdoc2 As HTMLDocument | |
'Variables initialization | |
loadingFrame = False | |
'Waiting for the frames | |
For i = 0 To HTMLdoc.frames.Length - 1 | |
Set myFrame = HTMLdoc.all.tags("FRAME").Item(i) | |
Do While (chrono1 < chrono2 And myFrame.readyState <> "complete") | |
DoEvents | |
chrono1 = TimeValue(Now) | |
Set myFrame = HTMLdoc.all.tags("FRAME").Item(i) | |
Loop | |
Set HTMLdoc2 = myFrame.contentWindow.document | |
Do While (chrono1 < chrono2 And HTMLdoc2.readyState <> "complete") | |
DoEvents | |
chrono1 = TimeValue(Now) | |
Set HTMLdoc2 = frame.contentWindow.document | |
Loop | |
loadingFrame HTMLdoc2, chrono1, chrono2 | |
Next i | |
'Time to free the variables | |
set myFrame = nothing | |
set HTMLdoc2 = nothing | |
End Sub |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment