vbams-accesspdffoxit-reader

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


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

Solution

  • 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.