0

With the .NET WebBrowser control we can do:

WebBrowser1.ObjectForScripting = new a_class();

where a_class is declared as [ComVisible(True)].

After which the instance of a_class is accessible to the javascript in the page under the name of window.external.

How do I achieve the same with the VB6/VBA WebBrowser OCX control?

There is apparently no direct property to set.

There are WebBrowser1.Document.parentWindow and WebBrowser1.Document.parentWindow.external objects, both are not null (provided the page has loaded), but assigning an instance of something to the latter results in

Runtime error 445
Object doesn't support this action.

Just to rule it out, I made the class I want to pass implement the IObjectSafety interface, but that didn't do anything (the IObjectSafety members are not even called).

I also tried inspecting the existing parentWindow.external object with TypeLib Information. ClassClassInfoFromObject fails with "Doesn't support automation or required interface," InterfaceInfoFromObject fails with "Subscript out of range."

GSerg
  • 76,472
  • 17
  • 159
  • 346

1 Answers1

0

This is not easy to do in VB6. You have to implement IDocHostUIHandler interface on the site object of the WebBrowser control and return an IDispatch from GetExternal method. The site object is provided by the VB6 form so you have to hook QueryInterface on its IUnknown to provide (some) custom implementation of IDocHostUIHandler on VB6's site object.

Here is direct copy/paste of an implementation I did some time ago: https://gist.github.com/wqweto/5065624

You'll need VBOleGuids3 typelib and a bit of code cleanup to make it compile as a separate project.

wqw
  • 11,771
  • 1
  • 33
  • 41
  • It would seem `VBOleGuids3` is only known to Google because you posted this answer :) Where can I get it from? Also, this won't work in VBA unless precompiled with VB6 because of no VBControlExtender support, right? – GSerg Mar 01 '13 at 16:37
  • I wouldn't dare this code in VBA. Try http://www.vbaccelerator.com/typelib.htm for typelibs -- will need `threadapi.tlb` too. – wqw Mar 02 '13 at 04:35