VERSION 1.0 CLASS
BEGIN
  MultiUse = -1  'True
  Persistable = 0  'NotPersistable
  DataBindingBehavior = 0  'vbNone
  DataSourceBehavior  = 0  'vbNone
  MTSTransactionMode  = 0  'NotAnMTSObject
END
Attribute VB_Name = "cX3WebBrowserNavAndWait"
Attribute VB_GlobalNameSpace = False
Attribute VB_Creatable = True
Attribute VB_PredeclaredId = False
Attribute VB_Exposed = False
' -- Xiphias3, September 09
'
' --This class waits for a WebBrowser control to completely
' load the requested webpage by waiting until the DOM of
' the requested page is completed.

' --Thanks to hitch for info on local files
'
' --Refer to KB Article: How To Determine When a Page is Done Loading
' http://support.microsoft.com/kb/q180366/

Option Explicit

Private Declare Sub Sleep Lib "kernel32.dll" (ByVal dwMilliseconds As Long)

Private m_bDOMReady     As Boolean
Private WithEvents m_WB As WebBrowser
Attribute m_WB.VB_VarHelpID = -1

Public Function navigateAndWait(ByRef ctlWebBrowser As WebBrowser, ByRef szURL As String)
    m_bDOMReady = False
    Set m_WB = ctlWebBrowser
    
    Call ctlWebBrowser.Navigate(szURL)
    Do While (Not m_bDOMReady)
        DoEvents
        Call Sleep(5)
    Loop
    
    Set m_WB = Nothing
End Function

Private Sub m_WB_DocumentComplete(ByVal pDisp As Object, URL As Variant)
    ' -In pages with no frames, this event fires one time after loading is complete.
    ' -In pages where multiple frames are loaded, this event fires for each frame
    ' where the DWebBrowserEvents2::DownloadBegin event has fired.
    ' -The top-level frame fires the DocumentComplete in the end.
    ' -So, to check if a page is done downloading, you need to check if the IDispatch* parameter
    ' is same as the IDispatch of the WebBrowser control.
    
    Dim oHTMLDoc As HTMLDocument
    
    If (pDisp Is m_WB.Object) Then
        m_bDOMReady = True
    ElseIf (TypeOf pDisp.Container Is HTMLDocument) Then
        Set oHTMLDoc = pDisp.Container
        If (oHTMLDoc.frames.length = 0) Then
            m_bDOMReady = True
        End If
    End If
End Sub
