Results 1 to 7 of 7

Thread: WebBrowser (If Page Fails To Load)??

  1. #1

    Thread Starter
    Lively Member
    Join Date
    Jul 2004
    Posts
    110

    Thumbs down WebBrowser (If Page Fails To Load)??

    I want to make it so if my webbrowser runs into a page that maybe doesn't exist and has a "This page cannot be displayed" then it will navigate to another.

    I only know the code to make it navigate to another.

    VB Code:
    1. WebBrowser1.Navigate (URL)

    Does anybody know the events I need to do this?

    Thanks alot!
    Last edited by ChR0NiC; Jun 3rd, 2005 at 03:38 PM.

  2. #2
    Banned dglienna's Avatar
    Join Date
    Jun 2004
    Location
    Center of it all
    Posts
    17,901

    Re: WebBrowser (If Page Fails To Load)??

    Look at this code.

    VB Code:
    1. ' Add component for Microsoft Internet Controls
    2.  
    3. Option Explicit
    4.  
    5. Dim blnBusy As Boolean ' <----------------Add this declaration
    6.  
    7. Private Sub Command1_Click()
    8. Dim i As Integer
    9.  
    10. 'Navigate to the page
    11. WebBrowser1.Navigate2 "http://www...coml" ' <-----Use your webpage
    12.  
    13. 'Both the readystate and the busy properties of the WB control
    14. 'are not completely reliable. The best way to wait for loading to take
    15. 'place I have found so far is using a custom boolean variable that
    16. 'is set in the BeforeNavigate2 and NavigateComplete2 events.
    17. blnBusy = True '   <---------------------Add these four lines
    18. Do While blnBusy = True
    19.     DoEvents
    20. Loop
    21.  
    22.  
    23. Private Sub WebBrowser1_BeforeNavigate2(ByVal pDisp As Object, URL As Variant, Flags As Variant, TargetFrameName As Variant, PostData As Variant, Headers As Variant, Cancel As Boolean)
    24. blnBusy = True
    25. End Sub
    26.  
    27. Private Sub WebBrowser1_NavigateComplete2(ByVal pDisp As Object, URL As Variant)
    28. blnBusy = False
    29. End Sub

  3. #3

    Thread Starter
    Lively Member
    Join Date
    Jul 2004
    Posts
    110

    Re: WebBrowser (If Page Fails To Load)??

    Sorry, but I don't understand how this can tell me whether the page failed to load or not.

  4. #4
    Fanatic Member TheVader's Avatar
    Join Date
    Oct 2002
    Location
    Rotterdam, the Netherlands
    Posts
    871

    Re: WebBrowser (If Page Fails To Load)??

    Check the NavigateError event. It will fire when problems occur when loading the site. Check the StatusCode to determine which error occurred; 404 is the one you're after here. The complete list of descriptions is here: http://msdn.microsoft.com/workshop/b...tuscodesvb.asp.
    Author for Visual Basic Web Magazine

    My articles on the Web Browser Control:
    Using the Web Browser Control & Using the DHTML Document Object Model

    The examples referenced in the articles can be found here:

  5. #5
    Fanatic Member TheVader's Avatar
    Join Date
    Oct 2002
    Location
    Rotterdam, the Netherlands
    Posts
    871

    Re: WebBrowser (If Page Fails To Load)??

    That error means your event declaration doesn't match the one of the control itself. In other words: the parameters in your declaration don't match with the ones set by the control. This probably happens because you're using a different Web Browser control version than the one that code uses. Don't directly paste from MSDN; use the NavigateError declaration that appears when you click the event in your IDE. That should handle it.
    Author for Visual Basic Web Magazine

    My articles on the Web Browser Control:
    Using the Web Browser Control & Using the DHTML Document Object Model

    The examples referenced in the articles can be found here:

  6. #6

    Thread Starter
    Lively Member
    Join Date
    Jul 2004
    Posts
    110

    Re: WebBrowser (If Page Fails To Load)??

    VB Code:
    1. Private Sub WebBrowser2_NavigateError( _
    2.     ByVal pDisp As Object, _
    3.     ByVal URL As Variant, _
    4.     ByVal TargetFrameName As Variant, _
    5.     ByVal StatusCode As Variant, _
    6.     ByRef Cancel As Boolean)
    7. If StatusCode = 404 Then MsgBox "I have failed you master"
    8. End Sub

    Procedure declaration does not match description of event.
    Thanks for nothing Microsoft.
    http://msdn.microsoft.com/workshop/b...igateerror.asp

  7. #7

    Thread Starter
    Lively Member
    Join Date
    Jul 2004
    Posts
    110

    Re: WebBrowser (If Page Fails To Load)??

    VB Code:
    1. Private Sub WebBrowser2_NavigateError(ByVal pDisp As Object, URL As Variant, Frame As Variant, StatusCode As Variant, Cancel As Boolean)
    2. MsgBox "I work"
    3. End Sub

    Works now, thanks everyone
    Last edited by ChR0NiC; Jun 3rd, 2005 at 04:20 PM.

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