3

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
Ricardo Diaz
  • 5,658
  • 2
  • 19
  • 30
  • Thread about it [here](https://social.msdn.microsoft.com/Forums/Lync/en-US/702899e9-7278-49b6-8253-0753542c6083/problem-displaying-pdf-file-using-web-browser-control-in-access-form?forum=accessdev) – Ricardo Diaz Feb 13 '21 at 03:03

2 Answers2

3

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.

Erik A
  • 31,639
  • 12
  • 42
  • 67
  • Nice! This has bothered me for years. Your solution is neat and clear and as you said, doesn’t need to relay on client specific settings Thanks! – Ricardo Diaz Feb 13 '21 at 11:07
  • Nice solution! It also helped me. – Harun24hr Feb 14 '21 at 09:03
  • @erik-a Internet Explorer stopped rendering the pdf files. Using Windows Pro 21H1 & Access 365 Version 2104 (13929.20372). Do you know a workaraound for this? – Ricardo Diaz Jun 12 '21 at 01:32
  • Sorry, Internet Explorer is EOL so things are bound to break eventually, for now I have no workaround and no alternatives yet – Erik A Jun 12 '21 at 06:14
1

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

Ricardo Diaz
  • 5,658
  • 2
  • 19
  • 30