CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 7 of 7
  1. #1
    Join Date
    Feb 2010
    Posts
    2

    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 -----
    Last edited by HanneSThEGreaT; February 17th, 2010 at 08:51 AM. Reason: [CODE] tags!

  2. #2
    Join Date
    Apr 2009
    Posts
    394

    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

  3. #3
    Join Date
    Jul 2001
    Location
    Sunny South Africa
    Posts
    11,283

    Re: How to know if InternetExplorer control has finished loading a multiframe web pag

    Welcome to the forums Rinku2010.

    When posting your code, please make sure to do so within &#091;CODE] and &#091;/CODE] tags.

  4. #4
    Join Date
    Jul 2006
    Location
    Germany
    Posts
    3,725

    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?

  5. #5
    Join Date
    Jul 2001
    Location
    Sunny South Africa
    Posts
    11,283

    Re: How to know if InternetExplorer control has finished loading a multiframe web pag

    Hello my friend!

    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
    Last edited by HanneSThEGreaT; February 17th, 2010 at 03:01 PM.

  6. #6
    Join Date
    Feb 2010
    Posts
    2

    Red face Re: How to know if InternetExplorer control has finished loading a multiframe web pag

    Quote Originally Posted by HanneSThEGreaT View Post
    Welcome to the forums Rinku2010.

    When posting your code, please make sure to do so within
    Code:
     and
    tags.
    Hi Hannes,
    Thank you for your reply and for advise about
    Code:
     and
    . 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

  7. #7
    Join Date
    Jan 2006
    Location
    Fox Lake, IL
    Posts
    15,007

    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
    David

    CodeGuru Article: Bound Controls are Evil-VB6
    2013 Samples: MS CODE Samples

    CodeGuru Reviewer
    2006 Dell CSP
    2006, 2007 & 2008 MVP Visual Basic
    If your question has been answered satisfactorily, and it has been helpful, then, please, Rate this Post!

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured