Hi,
I'm using the WebBrowserControl in an Access form to show HTML or PDF files.
When I right click on the control area I get the context menu on the left for HTML and on the right for PDF.
I would like to replace those menu with my own, but the WebBrowserControl doesn't expose a ShortcutMenuBar property as many other controls like ObjectFrame.
How can I accomplish that or at least to disable the above default ones?
Thanks, Lauro
Sub DisableRightClick()
Dim doc as Object
Set doc = MyWebBrowser.Object.Document
Dim scr as Object
Set scr = doc.createElement("script")
scr.Type = "text/javascript"
scr.innerhtml = "function disableRightClick(event) {event.preventDefault();}"
doc.head.appendchild scr
doc.body.setAttribute "oncontextmenu", "disableRightClick(event)"
End Sub
Try it out, let me know how it goes. Remember, of course, to call that procedure from an event of your form, like Form_Open or Form_Load.
Sub DisableRightClick()
Dim doc as Object
Set doc = MyWebBrowser.Object.Document
Dim scr as Object
Set scr = doc.createElement("script")
scr.Type = "text/javascript"
scr.innerhtml = "function disableRightClick(event) {event.preventDefault();}"
doc.head.appendchild scr
doc.body.setAttribute "oncontextmenu", "disableRightClick(event)"
End Sub
Try it out, let me know how it goes. Remember, of course, to call that procedure from an event of your form, like Form_Open or Form_Load.
Hi Edgar
I uploaded a sample database with one table and one mask.
You should insert the fullname of same html files.
Thanks again, for your interest.
Lauro
We have a different setup. Using your sample and writing the filepath triggers a download for me. Do you have the Adobe PDF plugin mentioned in this tutorial by chance?
We have a different setup. Using your sample and writing the filepath triggers a download for me. Do you have the Adobe PDF plugin mentioned in this tutorial by chance?
I'm not using a PDF plugin, but the Microsoft Access the WebBrowserControl .
As default browser I have Firefox 112.0.2 (64 bit), but I don't think it's relevant.
Try this one, it has two approaches, one with an embed tag and another with an object tag. I could only disable the browser's right click though.
Maybe check your pdf viewer's options to disable right click. Mine is acrobat reader and it has no option for that. I also tried applying an overlay div but the viewer overtakes the div and positions itself above. I didn't try that extensively, but there could be some other ways to accomplish what you need. It's important to mention that your viewer is what's allowing you to view the file. However, there are other approaches, maybe with an external library that supports ES5, or using an online viewer like Google Driver's viewer or some other viewer, you would have to upload your file there though.
Try to find a viewer that allows you to disable right click.
I have to mention that the attached file works on my end, it might not work on yours. If that's the case, let me know.
Try this one, it has two approaches, one with an embed tag and another with an object tag. I could only disable the browser's right click though.
Maybe check your pdf viewer's options to disable right click. Mine is acrobat reader and it has no option for that. I also tried applying an overlay div but the viewer overtakes the div and positions itself above. I didn't try that extensively, but there could be some other ways to accomplish what you need. It's important to mention that your viewer is what's allowing you to view the file. However, there are other approaches, maybe with an external library that supports ES5, or using an online viewer like Google Driver's viewer or some other viewer, you would have to upload your file there though.
Try to find a viewer that allows you to disable right click.
I have to mention that the attached file works on my end, it might not work on yours. If that's the case, let me know.
Thanks again Edgar.
I'm encountering the same error in both versions at line:
scr.innerhtml = "function disableRightClick(event) {event.preventDefault();}"
I found on the web the following file/ example that correctly disable right click in Firefox, Chrome and Edge.
Code:
<!DOCTYPE html>
<html>
<head>
<title>Example</title>
</head>
<body>
<h2>Disabling right-clicking on a webpage using JavaScript</h2>
<p> Right Click is disabled</p>
<script>
document.addEventListener("contextmenu", (event) => {
event.preventDefault();
});
</script>
</body>
</html>
When I try to visualize in my test TestHtm.accdb Access database, the WebControl blocks the execution of the script
If say Yes to the execution of the script I get syntatical errors at line 10 char 57 (the arrow function).
If I follow the What risks I could incour item menu, Internet Explore 11 opens at the page:
Scopri come usare e personalizzare i controlli ActiveX e ActiveX Filtering in Internet Explorer.
support.microsoft.com
So I guess that the Microsoft Access WebBrowserControl does simulate Internet Explorer behaviour. I inserted my example/file that disable the right click in IE (in the window opened by Access, because I was unable to find the path where is located IE.exe, "C:\Program Files (x86)\Internet Explorer\iexplore.exe" actually opens Edge) and again the script was blocked; when accepting the execution no errors were reported, but the script does not work.
I tried a few variations and I still haven't been able to do it with the Edge WebBrowser (Office 365). The code works fine in my IE WebBrowser though, the one I posted (Office 2016). It also works if you open it with the Edge Browser itself, not the one embedded in the web browser, I don't know if I'm explaining myself correctly. I tested a bunch of other javascript snippets and some work and others don't, I'm suspecting they butchered the Edge implementation for security or whatever.
Is sufficient to block the right click, without using any script tag. It was so easy to use in IE. I mean, sure, it does not support ES6, but a lot could be done without it. I'll try a few extra approaches in a few days but do keep me posted.