Oh you knew I would, admit it!
One of my biggest issues with the new Modern Web Browser control is this whole ‘Trusted Domains’ nonsense. It makes no sense to me to stop users from using a Web Browser to click on links to access other sites?! Isn’t that the whole point of a web browser, to browse, hence the name?! Instead of calling it a Web Browser control, it should have been called a Web Page Viewer control because now you aren’t supposed to use it to browse anything!
If you don’t know anything about the new browser or its limitations then check out:
So I have been trying to find some way to get around this limitation and, well, today I finally did.
Now this is a proof of concept that still needs a little more massaging, but in my brief testing it appears to be working quite well for links. It would still need further coding to handle buttons that act as links, … and other exceptions, but for a first step forward this is great. Things can easily be added and improved upon now that the basic idea works.
The Concept
The concept is quite simple. The Web Browser allow you to Navigate (in VBA) anywhere you want, yet normal web browsing is restricted. So I said to myself, after trying a variety of other things, what if there was some way to intercept browsing and instead redirect it to the VBA Navigate instead.
From that, I created:
The Code
So the code is broken down into 2 parts:
- Code to setup the page with the necessary code
- Code to retrieve the click link URL and Navigate to it
Code to setup the page with the necessary code
So we use the On Document Complete event, so that once the page is fully loaded we run code to inject:
- A JavaScript function to process clicks (JS – which capture clicks on the links)
- A hidden input (which is used to collect the click on link URL)
- A listener to capture any clicks on the page to run our injected JS function (the 1st item)
Private Sub EdgeBrowser0_DocumentComplete(URL As Variant)
Dim sCmd As String
sCmd = "var script = document.createElement('script');"
Me.EdgeBrowser0.ExecuteJavascript sCmd
sCmd = "script.innerHTML = " & Chr(10) & _
" function interceptClickEvent(e) {" & Chr(10) & _
" var href;" & Chr(10) & _
" var target = e.target || e.srcElement;" & Chr(10) & _
" if (target.tagName === 'A') {" & Chr(10) & _
" href = target.getAttribute('href');" & Chr(10) & _
" document.getElementById('clickedLinkHref').value = href;" & Chr(10) & _
" e.preventDefault();" & Chr(10) & _
" }" & Chr(10) & _
"}"
Me.EdgeBrowser0.ExecuteJavascript sCmd
sCmd = "document.body.appendChild(script);"
Me.EdgeBrowser0.ExecuteJavascript sCmd
sCmd = "var hiddenInput = document.createElement('input');"
Me.EdgeBrowser0.ExecuteJavascript sCmd
sCmd = "hiddenInput.type = 'hidden';"
Me.EdgeBrowser0.ExecuteJavascript sCmd
sCmd = "hiddenInput.id = 'clickedLinkHref';"
Me.EdgeBrowser0.ExecuteJavascript sCmd
sCmd = "document.body.appendChild(hiddenInput);"
Me.EdgeBrowser0.ExecuteJavascript sCmd
Me.EdgeBrowser0.ExecuteJavascript "document.addEventListener('click', interceptClickEvent);"
End Sub
Code to retrieve the click link URL and Navigate to it
Then, we use the Browser’s On Click event to retrieve the clicked URL from the hidden input (that we created earlier) and navigate to it.
Private Sub EdgeBrowser0_Click()
Dim sHref As String
Dim bNavigate As Boolean
sHref = Me.EdgeBrowser0.RetrieveJavascriptValue("document.getElementById('clickedLinkHref').value")
If Left(sHref, 7) = "http://" Or Left(sHref, 8) = "https://" Then
bNavigate = True
End If
If Left(sHref, 4) = "www." Then
bNavigate = True
sHref = "https://" & sHref
End If
If Left(sHref, 1) = "/" Then
bNavigate = True
sHref = GetBaseURL(Me.EdgeBrowser0.LocationURL) & sHref
End If
If bNavigate Then
sHref = WinHTTP_GetRedirectURL(sHref) 'Deal with redirects!
Me.EdgeBrowser0.Navigate sHref
End If
End Sub
So a little JS and VBA and we are back in business.
You can get a copy of the GetBaseURL() function from my article:
http://office.microsoft.com/en-us/templates/CT101527321033.aspx?av=ZAC
get/return:
http://office.microsoft.com
Well, it truly isn’t hard once you know how. …
and the WinHTTP_GetRedirectURL() from:
I’m still hoping Microsoft will see the light, either remove the Trusted Domains completely or at least provide a property to enable/disable the feature this way the developer could decide what is appropriate for their scenario.
Hopefully, this will help a few of you out there to make the New Web Browser control usable for actual navigation.
Wow, wow, WOW! Based on your earlier post and video and some testing I had done I was ready to write off the new control as simply unusable.
Thank you for figuring this out. I tested it and it works perfectly for my needs.
I must say, I originally thought the new control was the answer to my prayers, as they say. A 10 out of 10.
Then I started trying it out. Once I hit the Trusted Domains issue it quickly fell to a 6 out of 10. Then I did more advanced testing an found it doesn’t support all JavaScript, so it fell to a 5 out 10. With this workaround I think it brings it back up to a 6-7 out of 10.
All of that to say, I too am a little disappointed. They took a nice step forward, but why couldn’t they complete the job properly. As always, I’m assuming it is due to external constraints that the Access Dev Team have no control over. Wouldn’t be the 1st time.
Anyways, glad you find this useful.
hi
reading this article is nothing short of
thank you from the bottom of my heart.
you did a wonderful job.
on the other hand, the document is a nothing short of certificate of poverty for Microsoft.
I have identified the major reasons for this control to even called browser.
1. trusted domain.
2. zero access to the DOM.
3. too many issues with Java script.
the verdict.
Microsoft, go to the drawing table and give us a real edge browser control we can use, with documentation and examples.
You missed a couple issues, but yep, I’m also a little disappointed with Microsoft’s offering. I had expected a proper Web Browser control, one that could do everything Edge can do. Sadly, that is NOT what we got. Furthermore, I highly doubt we will see any improvements on the truly important issues. Bugs will be dealt with (eventually), but most of the true issues (Trusted Locations, No mixed content, JS not working), they will say are for security and will remain.
No one should have to implement workarounds to begin with. If the intention is to restrict users from wandering to other URL’s then a simple parameter should be available to enforce that condition.
That’s why I mentioned it and already suggested it to Microsoft.
Great job! Thanks for making the new Edge Browser useable!
Hello,
I’m trying to use this formula to get the edge browser to work.
I’m at the point where I’m inserting WinHTTP_GetRedirectURL() part, and whenever I try to run it, what I get is an error saying:
Compile-error User-defined type not defined.
and it points me to this part of the code:
Dim oWinHttp As WinHttp.WinHttpRequest
Could you help me out with this? Thank you
You need to add the ‘Microsoft WinHTTP Services, version X.X’ reference library or switch it to Late Binding.
It works now. Thank you.
You’re a lifesaver.
My pleasure.
Hi
i have followed your instructions and got all code in place
the EdgeBrowser0_DocumentComplete eventlistener “click” goes through properly.
for some reason the EdgeBrowser0_Click will not work
i put a break on
sHref = Me.EdgeBrowser0.RetrieveJavascriptValue(“document.getElementById(‘clickedLinkHref’).value”)
the program don’t seam to stop there or get the sHref
i have added error stem and it does not capture it
i run debug and no errors.
i have office 365 the latest and the browser work good with the intital “source control”
Any Idea?
Thanks
I will try to put together a standalone demo in the next couple of days.
You’re in luck, I had already created the database (just hadn’t posted it yet). Until I get it online here, you can quickly grab a copy from my OneDrive at: https://1drv.ms/u/s!AjYnefG2siYSkQLJgOvPFY6IX_f5?e=r95xZz. Just remember, the page has to be fully loaded before this setup will work.
Also, I have found the Edge Browser click event to be shall we say temperamental and not always reliable. Sometimes I’ve had to close the form and reopen it to get it to respond properly, I’m still investigating as I am hoping to find a reproducible scenario, up until now it seems random.
I have been using the “Access Modern Web Browser Control” on my forms for a long time with no real issues, but last week Microsoft did some updates, and not I can’t seem to get them to work. Is anyone else having new issues?? the browser is just freazing. 99 % of the time it doesnt show any site.
I haven’t played with the control in several weeks, so I can’t really comment. I try performing an Office Repair and see if that doesn’t help. If it doesn’t then rollback you build no. and wait for MS to cleanup their mess.
how can i rollback the build, i looked for info on that and i did not find anything that worked.
thanks again for your help.
You may like to look over:
thanks for your help. rollback fixed it. 🙂