0

I try to embed web page into .xlsm Excel 2010 file. That what I have made so far :
In Sheet1 I've inserted ActiveX control called 'Microsoft Web Browser' its default name is WebBrowser1 (Developer->Insert->More Controls->Microsoft Web Browser). Then using VBA editor I placed following code into sheet module Sheet1 :

Private Sub Worksheet_Activate()
WebBrowser1.Navigate "http://stackoverflow.com/"
End Sub

I've tried various websites and html files placed on my local hard drive but the content displayed in WebBrowser1 control is always the same - 'Internet Explorer Cannot Display The Webpage' error with no error from Excel VBA alone. It seems that Microsoft Web Browser Object can't establish connection to web page.

2 Answers 2

7

try this:

    'declare the web browser object for future reference and / or for listening to its events
    Dim WithEvents ie As WebBrowser

    'navigate when the worksheet is activated.
    Private Sub Worksheet_Activate()
        Set ie = ActiveSheet.WebBrowser1
        ie.Navigate2 "http://stackoverflow.com/"
    End Sub
Sign up to request clarification or add additional context in comments.

2 Comments

what does declaring with ` Dim WithEvents` change for properties of WebBrowser object ?
@Qbik The statement Dim WithEvents let's register the object in question so VBA can listen to it's event, much like for the worksheet object that expose by default it's events (onActivate , SelectionChange , ecc.). So for the WebBrowser , for example , you will be able to code on the OnQuit , onDownloadComplete events , and many more. It doesn't actually change it's property .
-1

After Navigate you have to put parenthesis maybe that's why it cannot read the site. It should be like

WebBrowser1.Navigate("http://www.stackoverflow.com")

1 Comment

This isn't true. Either syntax is supported.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.