excel 2016 UserForm vba webBrowser control: how to detect that no web page has been found for a (defective) URL?

I have a webbrowser control working in Excel UserForm.

If I call it with a legitimate url, I get the web page displayed:
Me.WebBrowser1.Navigate "http://bbc.co.uk"

If I use an erroneous url, the control displays a coloured text message that the page for that url has not been found.
I would like some code that tells me that the url was not found, so I can reset the form and ask the user to enter a new URL.

Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

ste5anSenior DeveloperCommented:
In simple cases you may use the LocationUrl, it would be different then. But the normal approach is using the NavigateError event.

Bind the web browser control to a private variable WithEvents, then you can hook into that event.

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
Kelvin4Author Commented:
Kelvin4Author Commented:
UserForm.name = frm_webBrowser1
.Control.name =  webBrowser1

Following @ste5an, I used _NavigateError as follows:

In the code sheet for frm_webBrowser1 I  placed:

Private Sub WebBrowser1_NavigateError(ByVal pDisp As Object, URL As Variant, Frame As Variant, StatusCode As Variant, Cancel As Boolean)
    Dim str As String
    str = "No web page available for url: " & vbCrLf & vbCrLf & URL & vbCrLf & vbCrLf 'WebBrowser1_NavigateError url = " & URL & " statusCode = " & StatusCode
    str = str & "if the url can be corrected NOW: click 'OK', else click '"
    MsgBox str, , "Web Browser: unknown URL"
End Sub

This trapped urls for unrecognised web pages, and allowed urls of actual web pages.
I had no need to bind the web browser control to a private variable WithEvents, probably because the Following event  was already available  in the VBE for WebBrowser1.

It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today

From novice to tech pro — start learning today.