2

In the past I have used the Microsoft WebBrowser control in many VB6 and Excel VBA forms. I need to do it again, but I can't figure out how to do it.

I tried opening an old VB6 project and I got this error message:

Line 17: Class SHDocVwCtl.WebBrowser of control WebBrowser was not a loaded control class.

I tried adding a reference to Microsoft Web Browser to a new VBA project, but it doesn't exist.

I tried adding a reference to msi.dll as suggested here, but didn't help.

I tried from the Developer ribbon - Insert - More Controls - Microsoft Web Browser, it does exist, but it says Cannot insert object.

I tried using the Shell.Explorer.2 object as described here, but I get an error:

Sheet1.OLEObjects.Add ClassType:="Shell.Explorer.2", Left:=147, Top:=60.75, Width:=400, Height:=400

Run-time error '1004': 
Cannot insert object.

The only way that worked was opening an IE window, but I need the browser to be embedded in a form because I need to add a few custom buttons:

Set IE = CreateObject("InternetExplorer.Application") 
IE.Visible = True
stenci
  • 8,290
  • 14
  • 64
  • 104
  • 2
    It is no longer supported for security reasons - at least in 2016. I think. Don't know if anything useful in [this](https://social.msdn.microsoft.com/Forums/en-US/f33240d1-8865-4bff-b1d0-0e7f0b841249/cannot-insert-webbrowser-in-2016-excel-for-use-in-vba?forum=exceldev) – QHarr Jul 24 '18 at 14:40
  • @QHarr Do you know of any alternative? – stenci Jul 24 '18 at 14:40
  • I don't I'm afraid but there are much more experienced developers who may know! – QHarr Jul 24 '18 at 14:42
  • Possible duplicate of [How to make Microsoft Web Browser object work in Excel 2016](https://stackoverflow.com/questions/35024440/how-to-make-microsoft-web-browser-object-work-in-excel-2016) – Our Man in Bananas Jul 24 '18 at 14:43
  • @stenci: see [Microsoft Support: Cannot insert certain scriptable ActiveX controls into Office 2013 documents](https://support.microsoft.com/en-gb/help/2793374/cannot-insert-certain-scriptable-activex-controls-into-office-2013-doc) – Our Man in Bananas Jul 24 '18 at 14:45
  • Haven't done this for a while so don't remember how to shorten the links but do these help? [Use FireFox](https://www.makeuseof.com/tag/windows-timeline-chrome-firefox/) or [Another alternate](https://stackoverflow.com/questions/19600586/excel-vba-create-an-embedded-webbrowser-and-use-it) – Zac Jul 24 '18 at 14:46
  • 1
    @Zac: it's [*description*](**url**) – Our Man in Bananas Jul 24 '18 at 14:47
  • 2
    I currently use `Microsoft Web Browser`(`ieframe.dll`) embedded in a form. Works without an issue on Office 2016 for me, so it appears to be possible. – Victor K Jul 24 '18 at 14:47
  • @OurManinBananas : thanks mate – Zac Jul 24 '18 at 14:49
  • @VictorK I have not listed it in my post, but I tried that too and it doesn't work for me – stenci Jul 24 '18 at 14:59
  • @stenci do you get any errors when you try `ieframe`? – Victor K Jul 24 '18 at 15:22
  • @VictorK No error message. I thought it didn't work because I was expecting to see the little globe in the form toolbox. Apparently something has changed and the toolbox doesn't show the new control, but it is usable from the code. See my answer for the steps required to get it to work. – stenci Jul 24 '18 at 15:38
  • 1
    @stenci Just curious: did you right click the toolbox to add the control? – Victor K Jul 24 '18 at 15:46
  • @VictorK No I didn't!!! – stenci Jul 24 '18 at 15:52
  • @stenci do I take it as you have found the control and now have it on its right place in the toolbox? – Victor K Jul 24 '18 at 15:53
  • @VictorK Yes, it is there. I edited the answer. – stenci Jul 24 '18 at 15:56
  • @stenci Glad that it got figured out! – Victor K Jul 24 '18 at 16:18

1 Answers1

4

I was not able to get the good old Web Browser control to appear in the VBA control toolbox as it used to, but I was able to dynamically insert it in a form from the code. So I can't drag the little globe into a form, but that's not a problem.

Some comments mentioned changing a value in the registry. That is required in order to insert an embedded browser inside a worksheet, but has no effect on the form.

It was enough to add a reference to Microsoft Internet Controls (ieframe.dll), then use the following code in a form:

Option Explicit

Private WithEvents WB As WebBrowser

Private Sub UserForm_Activate()
  Set WB = Me.Controls.Add("Shell.Explorer.2")
  WB.Navigate "http://google.com"
End Sub

Private Sub WB_NavigateComplete2(ByVal pDisp As Object, URL As Variant)
  Debug.Print "WB_NavigateComplete2"
End Sub

EDIT

@VictorK in a comment reminded me that I need to right click on the toolbox to add the control. I did remember about the control being added there after adding the reference, but I didn't remember (nor think) to right click and add the control.

So... all I needed to do was to add a reference to ieframe.dll instead of Microsoft Web Browser, just because Microsoft Web Browser doesn't appear in the list. Everything else seems to be working fine.

I still don't know why the old VB6 application doesn't load, but at this point I really don't care.

stenci
  • 8,290
  • 14
  • 64
  • 104