Previewing pdf files in MS Access and Foxit Reader webbrowser control fires print event

1.9k views Asked by At

Long story short, when you use a Web browser control and VBA to open a pdf file embbeded in a form, the pdf reader fires the print event automatically.

Current setup Win1064Bit/Office365 version 16.0.13628.20234 / Foxit Reader

Here is a screenshot to illustrate what happens

enter image description here

The event is so annoying that it's fired not once, but twice.

Code used to open the PDF file

Private Sub Command2_Click()
    Me.WebBrowser0.Navigate2 "C:\Temp\Sample.pdf"
End Sub
3

There are 3 answers

0
hermeslm On BEST ANSWER

Maybe it's too late but I would like to share with you my experience trying a PDF-embedded viewer in access but not limited only to viewing or printing PDFs, I even managed to fill out PDF forms and persist them in a local path or server (could be the CLOUD or a local server, it is up to you)

The Idea

Thanks to the help of Erik A who showed me the way. My solution was using a Web Browser Control that would allow me to display a Web page. This allows you to view a PDF, and even fill out a PDF form but is limited only to downloading the modified PDF (using the PDF viewer installed on the PC Adobe Reader or Foxit Reader)

But in my case, I needed to be able to save the modified PDFs without downloading them, it is also possible to print.

The Solution

Now with Microsoft's new release of the Edge Browser Control you can emulate a Browser using the latest Microsoft Edge(Full features and compatibility with new Web standards, by the way, the old IE Browser Control does not work)

Note!!! WebView2 (Edge Browser Control) is not installed by default or compatible with all Windows and Office versions, please read this post and check your configuration first.

enter image description here

Now to persist the modified PDFs in the backend(local server or Cloud) I created a Simple PDFs File Server with Node, Empress and using Adobe PDF Embed API.

The final result

This view allows you to save the modified PDF to the Simple PDFs file server listening in HTTP://localhost:3000 using the save button.

enter image description here

File saved from the Simple PDFs File Server using the "Save" button.

enter image description here

In this view, you can print and download the modified PDF.

enter image description here

MS Access code

Private Sub loadDoc()
    Dim docsFullPath As String
    Dim filePath As String
    Dim fileName As String

On Error GoTo ErrorHandler
    DoCmd.Hourglass True
    Me.Requery
    fileName = getFileName(Me.docPath) 'util function to extract file name
    filePath = getFilePath(Me.docPath) 'util function to extract file path
    docsFullPath = "http://localhost:3000/index.html?filePath=/" & filePath & "&fileName=" & fileName & "&title=PDF%20File"
    'docsFullPath = "http://localhost:3000/index.html?filePath=2022/815/1259&fileName=20240220-40.pdf&title=PDF%20File"
    Debug.Print docsFullPath
    If docsFullPath <> "" Then
       On Error Resume Next
       DoEvents
       Me.WebBrowser17.ControlSource = docsFullPath
       DoCmd.Hourglass False
    End If
    Exit Sub
ErrorHandler:
    DoCmd.Hourglass False
    MsgBox "Error #: " & err.number & vbCrLf & vbCrLf & err.description
End Sub

Conclusions

Thanks to the new Microsoft Edge Browser Control and Adobe PDF Embed API you can embed in MS Access PDFs without limitation and fully compatible. You can visit the Adobe PDF Embed API doc for more details about this API customization and features.

1
Ricardo Diaz On

Change the Foxit Reader preferences like this

  • Open Foxit Reader

  • Go to File | Preferences | Documents

  • Uncheck "In web browser, display PDF in Read Mode by default"


enter image description here

4
Erik A On

If you want to embed a file, you need to use HTML. Else, you have the default "download on navigate" behaviour unless a specific plugin allows it.

I use the following code to create a simple web page that displays the pdf:

Dim wb As Object
Set wb = WebBrowser0.Object 
Dim fileLocation As String
fileLocation = "C:\Temp\Sample.pdf"
wb.Silent = True
With wb
    .Navigate2 "about:blank"
    Do Until .ReadyState = 4 '=READYSTATE_COMPLETE
        'This is a somewhat inefficient way to wait, but loading a blank page should only take a couple of milliseconds
        DoEvents
    Loop
    .Document.Open
    .Document.Write "<!DOCTYPE html><HTML><HEAD><TITLE>My title</TITLE></HEAD><BODY scroll=""auto"" style=""margin: 0px; padding: 0px;"">" & _
                        "<embed src=""" & fileLocation & """  width=""100%"" height=""100%"" />" & _
                        "</BODY></HTML>"
    .Document.Close
End With

This works with Adobe Reader, Foxit, or pretty much any PDF viewer that supports viewing PDFs inside Internet Explorer.

Don't go adapting PDF viewer settings to work with your application. Instead, create an application that will work with all PDF viewers, to avoid loads of trouble if a user actually uses that PDF viewer and wants the settings to be different, or wants a different PDF viewer.