WebBrowser Control

I’ve never used the WebBrowser Control before the other day, but now I can’t stop using it. I was trying to find a way to display a PDF on a userform. I kept messing with the Adobe controls, but not everyone in my office has Acrobat loaded. Then I read that you can use this WebBrowser control.

To put a Webbrowser control on a userform, right click the Control Toolbox and select Additional Controls

Webcontrol3

Scroll down to Microsoft Web Browser and put a check next to it.

Webcontrol2

Then you can drag the control onto your userform and control it just like an instance of InternetExplorer. I forgot all about my PDF project and put this new knowledge to some real use. Two ListBoxes, each with all 117 college football teams, and two WebBrowser controls. Selecting a team in a ListBox will bring up that team’s schedule.

Webcontrol1

Here’s the code behind the ListBoxes. It’s not quite right yet. Things get a little screwy when you start rapid-fire selecting teams. And, as always, automating IE is slow.

Private Sub ListBox1_Change()
    Me.WebBrowser1.Navigate2 smURL & Me.ListBox1.Value
   
    Do
        DoEvents
    Loop Until Me.WebBrowser1.ReadyState = READYSTATE_COMPLETE
   
    Me.WebBrowser1.Document.body.doscroll
    Me.WebBrowser1.Document.body.doscroll
   
End Sub

I really don’t know what the difference between Navigate and Navigate2 is, but the example I saw used Navigate2. Someday, I’ll have to learn the difference. You can download this example workbook: WebControl.zip

16 Comments

  1. Harald Staff says:

    It IS another instance of Internet Explorer -just without the toolbars and menus.

    Navigate2 was a new method in IE4, extending the existing Navigate method “to allow for shell integration” as MSDN says. New properties – new name, that’s really decent.

    The web browser has a couple of very useful events: The BeforeNavigate event, where one can cancel the operation if it’s a unwanted URL, or start Word instead if it’s to a .doc, and the DocumentComplete event, as an alternative to the READYSTATE_COMPLETE Loop.

  2. Woudloper says:

    It’s nice. I am using the ‘Microsoft Internet Controls’ and the ‘Microsoft HTML Object Library’ for a whil now to import certain data (from a table) in a worksheet. I am even using the formelements to navigate (with certain parameters) to the actual page I need.

    For the ReadyState Complete I am using the DocumentComplete feature (as you described) as followed:

    Private WithEvents webbrowser As SHDocVw.InternetExplorer
    Private blnDocComplete As Boolean

    Private Sub webbrowser_DocumentComplete(ByVal pDisp As Object, URL As Variant)
    blnDocComplete = True
    End Sub

    Within the actual function in which all the stuff is being handled I am using the following loop situation:

    Do While Not blnDocComplete
    DoEvents
    Loop

    Maybe this has any use for you?

  3. Chris Graham says:

    Has anyone figured out how to print from the WebBrowser through VBA? When I have tried some code from MSDN which was really for VB (http://msdn.microsoft.com/workshop/browser/webbrowser/webbrowser.asp) , it did not seem to work correctly and I got a run-time error. Thanks!

  4. Mahesh Karnik says:

    I am new to excel programming so please bear with me. I am trying to display a simple HTML document (with hyperlinks) that was created from word. I want it displayed on a worksheet such that the user can see it and scroll down either using the scroll button on the embedded box (not the excel sheet scroll down button) or using the hyperlinks I have created that take the user to the different sections of the document. I have excel 2000 and I tried dragging the HTML file on the web browser control that I created on the sheet. But it does not work. Any suggestions will be greatly appreciated.

  5. Jean-Fran├žois Gay says:

    I have had a problem with the WebBrowser control in VBA. Every time I have a break point between the first instance is createdand the browsing to a wab page, the WebBrowser control freezes, freezing everything (including AutoCAD and the rest of my forms loaded on the screen; in other words, the entire application freezes). I have tried it on some different OS, and on some different version of VBA (excel, Word, VisualUML) and got the same problem. In Visual Basic, tought, it does not freeze at all. Is there some kind of possible issue between VBA and the WebBroser control?

  6. Derfel Cadern says:

    It’s a great option to use but it has one huge problem that I havan’t been able to solve yet. Perhaps somebody here has an idea.
    I’ve added a Multi-page-control to a form and a webbrowser-control to tab no# 1. I can hide the webbrowser, when a user clicks on one of the other tabs. But when I click on tab no# 1 the webbrowser isn’t shown. I’ve tried “WebBrowser1.Visible = True”, but I get the errormessages.

    Private Sub MultiPage1_Change()
    If MultiPage1.Value = 0 Then
    WebBrowser1.Visible = True
    Else
    WebBrowser1.Visible = False
    End If
    End Sub

    Private Sub TextBoxUsername_Change()
    URL = “http://feedback.ebay.com/ws/eBayISAPI.dll?ViewFeedback&userid=” & TextBoxUsername.Value
    ‘URL = “C:Dokumente und EinstellungenPietro SforzaEigene DateienM$-Office-DocumentseBayWebsMyIndex.html”
    Me.WebBrowser1.Navigate2 URL
    Do
    DoEvents
    Loop Until Me.WebBrowser1.ReadyState = READYSTATE_COMPLETE

    End Sub

  7. chrisix says:

    Hi

    Sorry… “microsoft webbrowser” is not in my control list !
    Is it in a particuliar version of excel ?

    Thanks

  8. manan says:

    Hey hi friends …i face one problem in webbrowser control and tat is whn i use the Private Sub webbrowser_DocumentComplete(ByVal pDisp As Object, URL As Variant)
    it executes whenever a frame on a particular website is loaded…….But i want something which executes only after the whole webbrowser has done loading ……even the
    Do
    DoEvents
    Loop Until Me.WebBrowser1.ReadyState = READYSTATE_COMPLETE
    End Sub

    Doesnt work in these case……could u please help me guys

  9. ALeXceL says:

    To chrisix: i was experiencing the same problem. I found (in brazilian portuguese language…) “Navegador da Web da Microsoft”, instead of the simple “Microsoft Web Browser”. Add mshtml.TLB and shdocvw.DLL to your project references and then try adding something like that.

  10. Ginja75 says:

    Hi there all, I’m having a huge problem with the web browser in excel.
    Me and some colleagues are putting a database together for a new contract and we’ve managed to build a working database in Excel (for some reason our IT dept wouldn’t allow the use of Access).
    Anyway we’re using the multipage facility for the userforms and we want to use a web browser for at least one of these pages, which works fine until we select one of the other pages and then the browser disappears.
    If I run debug, then I get an error message about “Navigate” and “IWebBrowser2?.
    Is there a way to keep the web browser open while running other elements of the same database.
    I’ve used the hide form facility and this works fine, but this defeats the objective of using this in the first place.

    Any help on this is appreciated.

  11. Andrew Moore says:

    Ginja75 and Derfel Cadern:

    I just struggled with this as well. The solution is that the WebBrowser control cannot be placed on a page within a MultiPage. You must put the WebControl on the main form, and then use code for hiding and showing the WebBrowser.

    For example, if you want the webBrowser displayed on page 2, you can do this:

    Private Sub MultiPage1_Change()
    If Me.MultiPage1.SelectedItem.Name = “Page2? Then
    Me.WebBrowser1.Visible = True
    Else
    Me.WebBrowser1.Visible = False
    End If
    End Sub

    (there’s probably a better way of figuring out which page is selected)

    Any appropriate calls to Navigate (or navigate2) can also go in this event handler.

    Make the WebBrowser the same size as the client area of you MultiPage, but do not put it on the MultiPage (you shouldn’t be able to see the WebBrowser in design mode if you did it right).

    One more thing – be sure to set the Visible property of the WebBrowser to False in design mode to make the magic complete.

  12. john says:

    Im having trouble with a web browser object!
    Am trying to get a command button to return the viewed web site address into a cell (the cell that the web box will then get its navigate2 address from.
    the idea being that the user can navigate to a site using the onscreen links and then set that site using the command button to an active cell offset cell, therefore whenever the user clicks on that same active cell the web site they chose will appear.
    It all works except i cant get it to return the current HTTP address!n any ideas

  13. Warwick says:

    Hi John,

    you’ve probably solved this by know, but this should work…

    Cells(ActiveCell.Row, 4).Value = UserForm1.WebBrowser1.LocationURL
  14. Jazz says:

     I am facing problem with Export to Excel Button which has no instance in View source coding of Web Page. Can anyone let me know how can we get that button reference so that i can further use .click for further processing.

    <p>Thanks,</p>

  15. KalugE says:

    Hi John, you mean ActiveCell.Offset(0,4).value
    rigth?

    keep up

  16. Rajesh says:

    Hi,

    I want to copy an image from website through “userform1.webbrowser1? can any one assist me with code ????

Posting code or formulas in your comment? Use <code> tags!

  • <code lang="vb">Block of code goes here</code>
  • <code lang="vb" inline="true">Inline code goes here</code>
  • <code>Formula goes here</code>

Leave a Reply

Here's how to update your reports of company and nearly any web data: