Webbrowser Control - wait till document is complete fully, properly, with frames etc

Posted on 2009-04-23
Last Modified: 2013-11-19
I have the webbrowser control on  a MSACCESS Form.
I am navigating to an internal webserver.
Each page has many frames.
The problem is my script is running (looking for things in the DOM) before the document is loaded.  I feel it is because a lot of the time only a frame changes.  The URL does not change.

I have read this forum and am aware of;en-us;180366

Further for example or

I have tried this
Private Sub WebBrowser0_DocumentComplete(ByVal pDisp As Object, URL As Variant)
If (pDisp Is WebBrowser0.Object) Then
 do the next thing
end if
end sub
This fires all the time (could this be that the webbrowse is getting the info from the cache

Reading the Microsoft Info in c++ they also give a solution for Frames at the bottom of that page.
I thus declared a public object - being  NAV_OBJ

and had this (NavigateComplete2) was only firing once in my testing but i am aware I need to only get the first object.

Private Sub WebBrowser0_NavigateComplete2(ByVal pDisp As Object, URL As Variant)
     Set pDisp = NAV_OBJ
End Sub

then i had

Private Sub WebBrowser0_DocumentComplete(ByVal pDisp As Object, URL As Variant)
If (pDisp Is NAV_OBJ) Then
 do the next thing
end if
end sub

As far as I can work out these two objects never were equal

Also once getting the above solved I assume we will put into the above something like
if success downloadcomplete = true

and then to wait in a sub routine this

do while not downloadcomplete

For me MSAccess just sits in this loop and does nothing else and will not let the DocumentComplete to fire inorder to change the value of downloadcomplete.

<+++++++++++++++++++++MY TESTING SHOWS++++++++++++++++++++++++edited new information

setting a break on the download complete I can do quite a lot on this website without the break even happening so I need something else.

Is there some way to check that the HTMLDocument or the HTMLFRAME is ready.  I tried Document.readystate = "complete" but that did not seem to work.

Is there some way to track a HTMLDocuments state or a HTMLFrames state and then fire off events relating to that document.

edit: solved it myself as i did not get an answer from this website forum
Question by:darbid73
    LVL 8

    Expert Comment

    Did you try to check the URL in WebBrowser0_DocumentComplete()?
    If it is the URL that you provided on start, the document is fully loaded.

    Kind regards,

    Matthias Vance
    LVL 19

    Accepted Solution

    I think the solution to this problem is very specific.

    I am dealing with 7 frames here and sometimes one frame was dependant on another.

    There are two possible things to do - and I use a combination of both:

    First Declare this
    Public WithEvents HTMLFRAME4 As HTMLDocument

    Then in the document complete routine
    Set HTMLFRAME4 = myHTMLDoc.frames(4).Document

    Then you will have all of the events of a HTML Document available to you for example I then use

    Private Sub HTMLFRAME4_onreadystatechange()

    The second thing is that because it appears that some frames were dependant on others at certain times the only way to make sure I did not get errors was to check them all.

    for example in my case I checked all the frames and ONLY after all of them are ready do I give the green light.

    If myHTMLDoc.frames(1).Document.readyState = "complete" _
    And myHTMLDoc.frames(2).Document.readyState = "complete" And myHTMLDoc.frames(3).Document.readyState = "complete" _
    And myHTMLDoc.frames(4).Document.readyState = "complete" And myHTMLDoc.frames(5).Document.readyState = "complete" _
    And myHTMLDoc.frames(6).Document.readyState = "complete" Then



    End If

    To the above you could add the main document but I found that sometimes my main document did not ever get to a readystate of complete.  I do not know why.


    Featured Post

    IT, Stop Being Called Into Every Meeting

    Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

    Join & Write a Comment

    This is a PowerShell web interface I use to manage some task as a network administrator. Clicking an action button on the left frame will display a form in the middle frame to input some data in textboxes, process this data in PowerShell and display…
    JavaScript has plenty of pieces of code people often just copy/paste from somewhere but never quite fully understand. Self-Executing functions are just one good example that I'll try to demystify here.
    In this tutorial viewers will learn how to style transparent/translucent elements using alpha transparency in CSS Start with a normal styled element, such as a div.: Define its "background-color" property as "rgba (255, 255, 255, .5): The numbers in…
    The viewer will learn how to create and use a small PHP class to apply a watermark to an image. This video shows the viewer the setup for the PHP watermark as well as important coding language. Continue to Part 2 to learn the core code used in creat…

    728 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

    Need Help in Real-Time?

    Connect with top rated Experts

    16 Experts available now in Live!

    Get 1:1 Help Now