Hello everyone,

I've been trying to generate a way to catch or suppress errors in my web browser control. actually what i really need in my browser is that if i run my project and the page cannot be display, to generate an error handler so instead of showing "The page cannot be display" to show something else, like "about:blank" for example or not display anything. i tried this:

Dim WithEvents objDoc As MSHTML.HTMLDocument
Dim WithEvents objWind As MSHTML.HTMLWindow2
Dim objEvent As CEventObj

Private Sub WebBrowser1_NavigateComplete()
Set objDoc = WebBrowser1.Document
Set objWind = objDoc.parentWindow
End Sub

Private Sub objWind_onerror(ByVal description As String, ByVal URL As String, ByVal line As Long)
Set objEvent = objWind.event
objEvent.returnValue = True
MsgBox (description)
End Sub

But i would like something diferent or better than this.
Any help will be appreciate.
Thanks in advance

You should be able to check for the HTTP response code, either via an exposed property/function return of the WebBrowser control itself, or manually in the page headers. If the response code is an error (say 400, page not found) you could navigate "about:blank" or whatever else you want to do.

Thanks, but i don't like your suggestion... i'll find another way arround this.

You can try the following, just change the code to suit your needs -

Private Sub WebBrowser1_NavigateComplete2(ByVal pDisp As Object, URL As Variant)

Set objDoc = WebBrowser1.document
Set objWind = objDoc.parentWindow
End Sub

Private Sub objWind_onerror(ByVal Description As String, ByVal URL As String, ByVal line As Long)

Set objEvent = objWind.event

objEvent.returnValue = True
End Sub

Private Sub WebBrowser1_NewWindow2(ppDisp As Object, Cancel As Boolean)

Dim CtrlKey

If CtrlKey <> True Then
    Cancel = True
End If

Cancel = True
End Sub

Useless code, thanks anyway

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.