Reputation: 193
On Access 2007, is there a way to display the content of a PDF, even if it is just the first page, on a form? This PDF saved in a table as attachment.
Upvotes: 3
Views: 18878
Reputation: 3047
The only two methods I know of for previewing a PDF (WebBrowswer as suggested by Daniel and the Adobe Active X control) require a file path to be passed to the control.
I recommend extracting the file from the attachment field and saving it to a temporary location such as C:\Documents and Settings\username\AppData
. This can be found by using the vba Environ
command.
Extracting the file is done with the SaveToFile
method in the embedded DAO recordset (which is how attachments are stored in memory).
Assume each record has a field called AttachedFile
and each record has only one attached PDF. The form is using a WebBrowser control named PreviewBrowser
to view the PDF
Private Sub Form_Current()
On Error GoTo ExitSub
Dim FormRS As DAO.Recordset
Set FormRS = Me.Recordset
Dim RecAtt As DAO.Recordset
If (Me.AttachedFile.AttachmentCount > 0) Then
Set RecAtt = FormRS.Fields("AttachedFile").Value
RecAtt.OpenRecordset
Dim Path As String
FilePath = Environ("APPDATA") & "\Preview.pdf"
If (Dir(FilePath) <> "") Then Kill FilePath
RecAtt.Fields("FileData").SaveToFile FilePath
Me.PreviewBrowser.Navigate2 FilePath
End If
ExitSub:
RecAtt.Close
End Sub
Of course the code needs to be a bit more complicated if there are multiple attachments to a given record, but that would be done by manipulating the RecAtt
recordset.
Upvotes: 0
Reputation: 13122
Disclaimer: This answer will only work for PDF files stored outside of your database as separate file. They can be located over a network connection, but I do not know how to access them directly from your database table. This site gives a thorough guide to using the attachments, but doesn't show how to actually display them automatically. It is likely functionality not provided by Access.
You can display anything Internet Explorer can display with a Microsoft Web Browser
Control.
Once you've added the control, you can navigate to whatever you want to display during the load or open event of the form.
For example, if the control is called WebBrowser0
then the following would work:
Private Sub Form_Load()
Me.WebBrowser0.Navigate2 "C:\example.pdf" 'Substitute the actual address here.
End Sub
This is an extremely versatile method for displaying other content within Access. You can find more information here.
Upvotes: 5