2

I have a userfrom inside of which a VBA WebBrowser was inserted. I have working code that calls this browser, but is throwing errors because, I think, of the deprecated IE.

I have installed Selenium Basic and webdriver.exe. I can open Edge, but when opened its not inside the user form, let alone anywhere in a worksheet. Any pointers would be appreciated. Here is what I have:

From a cmdbutton on a worksheet Macro "ShowBrowser" is called.

Sub ShowBrowser()
   'Show the form.
    WebBrowser.Show vbModeless
End Sub

The form is named WebBrowser. From Design Mode, the Microsoft Web Browser is inserted. On the properties window, it is renamed to ObjWebBrowser.

From the Project Explorer, I open the form, click View Code, I have this:

Private Sub ObjWebBrowser_Initialize()
     Dim selenium_Obj As New Selenium.EdgeDriver
     Call selenium_Obj.Start("edge", "https://google.com")
    selenium_Obj.Wait 5000
End Sub

I have tried multiple approaches, but cannot find a way to open Edge inside the userform.

DecimalTurn
  • 3,243
  • 3
  • 16
  • 36
dlowrey
  • 35
  • 3

2 Answers2

0

The reason why it's relatively simple to embed IE inside a form is indeed due to the control that is provided in the Microsoft Internet Controls Library (ieframe.dll). (More details here)

enter image description here

Fig1: Library location

enter image description here

Fig2: Control location

To do the same with Edge, you'd need a similar library that provides that control, but SeleniumBasic does not provide a userform control for that, it (only) provides an API to interact with a running instance of the web browser. This means that it needs to open the application in a separate window.

Hence, Selenium is not the tool that will allow you to embed a modern web browser inside a VBA userform.

In order to embed one inside a form, you'd need to make your own control which is no small task especially since Edge isn't based on the ActiveX technology that made most of what is done with IE possible.

Instead, Edge uses WebView2 and as discussed in another question, no one as yet created a fully fonctional library to bridge the gap between WebView2 and ActiveX. Sam Sirry's comment mentions that there was work done regarding this. You can have a look at the original VBForums post about it or a more recent post, if that's an option you want to look into. Note that I haven't tested those options.

Nonetheless, if I were you, I would reconsider the need to embed the browser inside the form and try to figure out if showing the browser as a separate window is acceptable or otherwise if it's possible to capture the inputs needed from the user inside the userform and then send those input via HTTP to an API or via selenium running a browser in the background.

DecimalTurn
  • 3,243
  • 3
  • 16
  • 36
  • Well, that's disappointing news. Thank you for answering. – dlowrey Jul 03 '23 at 23:12
  • The reason for trying to use a UserForm is to keep the browser window on the right side of a worksheet to display a map, while allowing use of the cells on the left for data that dynamically add Pins on the map. Is there a way to confine a browser window to a range of cells on a worksheet? – dlowrey Jul 03 '23 at 23:14
  • @dlowrey - You could always move and resize the window manually or by using `SetWindowPos`. Related: https://stackoverflow.com/questions/51359645/how-to-change-screen-size-of-an-external-application-using-vba – DecimalTurn Jul 04 '23 at 04:31
0

I cannot find a way to open Edge inside the Excel userform

This is because Excel supports ActiveX objects and as @DecimalTurn showed the reference to Internet Explorer is ieframe.dll (or shdocx.dll).

You're trying to use Edge which is based on Chromium inside Excel and that isn't going to work.

Perhaps the old IE will? Give it a go:

Call selenium_Obj.Start("ie", "https://google.com")
Jeremy Thompson
  • 61,933
  • 36
  • 195
  • 321