Access 2010 WebBrowser DOM (1 Viewer)

Poussin

New member
Local time
Yesterday, 23:48
Joined
Mar 22, 2011
Messages
8
Hello everyone, this is my first post in this forum.
I have a form containing a webbrowser in my access 2010 database. This is the latest version of the webbrowser (that is, not the ActiveX version). I use it to display a html document, with a few hyperlinks.
I've been searching the web to find a way of getting input from the html document that could in turn trigger some events in Access. For instance, i'd like to be able to open a form by clicking a hyperlink. This turned out to be much more difficult to achieve than i initially thought.
I have found this page: (not allowed to post link, it's a page called Handling Events in Visual Basic Applications in Msdn), which is quite encouraging i think, though i can't get the onclick event to open the msgbox (probably because i wasn't able to set "my_default_method" as the default method in the class module in VBA). Incidentally, in my case i had to add ".object" after each instance of "wbMyBrowser" (this is required with the new native webbrowser in access 2010).

I would really appreciate any help or hint.

Cheers!
 

Banana

split with a cherry atop.
Local time
Yesterday, 23:48
Joined
Sep 1, 2005
Messages
6,318
I don't have 2010 in front of me to check this but I thought I saw a list of events natively supported by web browser when you use it in a client form, allowing you to respond to documents events such as onLoad or so forth. Or did you mean events associated with controls within rather than the document itself?

If latter, then I would imagine you'd need to reference MSXML library then set up events using objects from MSXML after you set the reference to the appropriate elements in the DOM.
 

Poussin

New member
Local time
Yesterday, 23:48
Joined
Mar 22, 2011
Messages
8
Thank you Banana for your reply.
Yes, the WebBrowser object does support a limited set of events, but none related to the objects within the document (hyperlinks, buttons, etc.).
I'm not familiar with the MSXML library at all, could you be a little more specific?

More generally, since the VBA code is always triggered by events happening on access objects, it is unclear to me how i can catch an event occuring at the document level.
 

Banana

split with a cherry atop.
Local time
Yesterday, 23:48
Joined
Sep 1, 2005
Messages
6,318
To add a reference to MSXML library, you need to go to the VBA Editor, Tools -> Reference, scroll down to "Microsoft XML n.n" and check it, then click OK. You may have either 3.0 or 6.0 already installed.

But don't bother. I just looked in the library and found that Entity & Element objects do not have any events, only the Document, which you already have.

Thinking about it more, I'm not sure if it's possible to register an event on an element of the document from VBA or any external source because within the browser, we are using a different engine. In case of Internet Explorer, that would be either VBScript or JavaScript. (I'd just forget about VBScript since this is not web-standards-compatible).

So, you'd need to write an HTML document that contains the JavaScript that will do the registering of the event handler, accepts an input from Access webbrowser (you can use a query string parameter or direct Access to write to an element where JavaScript has a appropriate event handler registered to respond to it correctly as example) so the JavaScript can do something, then when something happens, it can write to a hidden element for Access to read. That's not really event-based but the closest I can think, if my assumptions about elements' events not being exposed outside the scripting engine is correct.
 

darbid

Registered User.
Local time
Today, 08:48
Joined
Jun 26, 2008
Messages
1,428
Can you give us your URL? I assume what you would like to do is that on the webpage there is an element like a button or link and when that is clicked you would like it to fire something in your Access database.

If this is true I am pretty sure (depending on the element) that we can "hook" the event of the element. If we cannot we can use other events like "beforenavigate" or navigate" events to check the url and see what has been clicked or where the webpage is going.


mySoapBox - I have Zero experience with 2010 but I am pretty suer MS did not put anything too new into a/the webbrowser control. They certainly did not go to any trouble with the webbrowser control for their flagship WPF forms. I am guessing they just wrapped the old one in a new wrapper. Kinda like taking a 1970 Toyota that still runs and just giving it a shiny new paint job - but doing nothing to the interior or motor. I think Internet Explorer and thus the controls are dying will eventually die.
 

Poussin

New member
Local time
Yesterday, 23:48
Joined
Mar 22, 2011
Messages
8
Hi Darbid,

I 'm sorry i can't give you the url as i m using a local file in my webbrowser.
It's nothing fancy : a simple html page with a google map and some hyperlinks. Basically i have a list of people for a few countries, and their name is displayed in an info window. Each name is a link which should theoretically open the individual's profile in a form in Access when clicked.
Each <a> can be given a unique Id, so all i need is to find a way of trapping the onclick event and tell Access that hyperlink "Id" has been clicked.

Any idea?
 

darbid

Registered User.
Local time
Today, 08:48
Joined
Jun 26, 2008
Messages
1,428
So you are the owner of the HTML - then these hyperlinks do they actually work?
I am guessing not. Thus just hyper link to a fake place. At the end of the hyperlink have a number which represents the person clicked on in the html.

Trap the event in the BEFORENAVIGATE of the webbroser. Cancel navigation but parse out of the URL (which is your fake hyperlink) the number - now you can give this number to your database.
 

Poussin

New member
Local time
Yesterday, 23:48
Joined
Mar 22, 2011
Messages
8
Hi darbid,

It worked perfectly. Thank you so much! That was way more simple than i was expecting.
Cheers!
 

Banana

split with a cherry atop.
Local time
Yesterday, 23:48
Joined
Sep 1, 2005
Messages
6,318
darbid -

Thanks for sharing this. This definitely may come in handy in later.
 

darbid

Registered User.
Local time
Today, 08:48
Joined
Jun 26, 2008
Messages
1,428
darbid -

Thanks for sharing this. This definitely may come in handy in later.

It is actually not the first time I have suggested this. It is actually the only way to get a website to directly talk with VBA code (webbrowser control or IE). In .NET there are other ways to do this, but in VBA it is impossible.
 

Banana

split with a cherry atop.
Local time
Yesterday, 23:48
Joined
Sep 1, 2005
Messages
6,318
Right. Didn't have a reason to use web control (whether new 2010 native or the ActiveX one) so didn't know about its shortcoming. Thanks for confirming my assumptions.
 

darbid

Registered User.
Local time
Today, 08:48
Joined
Jun 26, 2008
Messages
1,428
Ahh - now A2010 might have changed things. I am not up with that really. It could be that 2010 has taken on some of the .NET ideas.
 

Banana

split with a cherry atop.
Local time
Yesterday, 23:48
Joined
Sep 1, 2005
Messages
6,318
FWIW, the document events are now native so when you add a WebBrowser control you can have access to those events just like you have with any other controls, but as far as I can see, it's basically only document events. Another poster on here said that the new 2010 webbrowser control appears to be very similar to the ActiveX, the difference being that you didn't have to use an ActiveX control to add it.

In one project, I was able to implement a means of communication from VBA to DOM via query string parameter but didn't had a need to do it the other way around.
 

Poussin

New member
Local time
Yesterday, 23:48
Joined
Mar 22, 2011
Messages
8
I think you're right Banana. You can access the document using :
Me.WebBrowser1.Object.Document ....

The problem is that i haven't been able to find any documentation on the methods or properties available in VBA. Have you found any?
Also, say we could use AddEventListener('click',SomeFunction()), do you think VBA would call the sub called SomeFunction?
 

Banana

split with a cherry atop.
Local time
Yesterday, 23:48
Joined
Sep 1, 2005
Messages
6,318
Well, you certainly can write to the document to alter the layout. I'm not sure whether writing in javascript commands will also execute it (I somehow doubt it, since there needs to be some kind of trigger for the browser to invoke its scripting engine). Essentially you'd just google on how web developer usually dynamically modify their documents and adapt that. Remember, it's just a text document that's parsed by engine so all you have to is put in the right text in right place at right time, essentially.
 

darbid

Registered User.
Local time
Today, 08:48
Joined
Jun 26, 2008
Messages
1,428
what are you trying to do? Are still trying to hook an event.

I am pretty sure you will need to use WithEvents to do this.

That is get the HTML document > Get the object you want to listen to.

The difficulty in doing this is that you have to do it each time the HTML document changes.
 

darbid

Registered User.
Local time
Today, 08:48
Joined
Jun 26, 2008
Messages
1,428
AddEventListener('click',SomeFunction()), do you think VBA would call the sub called SomeFunction?
if SomeFunction is a J Script function in your HTML then yes VBA can call this function and give it parameters.

Communication from VBA to your HTML document is very possible.

Communication from the HTML back to VBA is not so easy. (Unless and I doubt it that A2010 has done something NEW with the IE control.

I dont suppose you could put together a HTML page and A2010 example of using it and post it. I have access to A2010 and would have a go at it. I have never used it so no promises.
 

Users who are viewing this thread

Top Bottom