Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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
like image 602
Ricardo Diaz Avatar asked Nov 29 '25 02:11

Ricardo Diaz


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

like image 200
Erik A Avatar answered Nov 30 '25 23:11

Erik A



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!