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

Viewed 938

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
2 Answers

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.

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

Related