VBA code in Access report to export a copy of itself to a PDF file


I am using Access  (Office 365) 64 bit

I have created a report in access.  I would like to have some code on the Report Open event that would export a PDF version of itself to a predetermined folder.  I expect that the formatting will be preserved in the export to PDF.

The name of the report is "Qry Temp".

I would appreciate your help
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

In VB.Net
Imports Microsoft.Office.Interop

Dim accessApp As Access.Application = New Access.ApplicationClass
Dim reportCode As String = "Qry Temp"
Dim outputToPathFile As String = "C:\YourReport.pdf"

' Open Instance of MS Access Application
accessApp.OpenCurrentDatabase("C:\YourMsAccessDatabase.accdb", False)

' Run Report
accessApp.DoCmd.OpenReport(reportCode, Access.AcView.acViewPreview)
accessApp.DoCmd.OutputTo(Access.AcOutputObjectType.acOutputReport, reportCode, Access.Constants.acFormatPDF, outputToPathFile, False, , , Access.AcExportQuality.acExportQualityPrint)
accessApp.DoCmd.Close(Access.AcObjectType.acReport, reportCode, Access.AcCloseSave.acSaveNo)
' Clean up
accessApp = Nothing

Open in new window

peispudAuthor Commented:
Thank you for the code.    I was hoping for Access VBA  though
Sorry in VBA it is:
Const reportCode As String = "Qry Temp"
Const outputToPathFile As String = "C:\YourReport.pdf"

Private Sub Report_Open(Cancel As Integer)
  DoCmd.OutputTo acOutputReport, reportCode, acFormatPDF, outputToPathFile, False, , , acExportQualityPrint
End Sub

Open in new window


Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
peispudAuthor Commented:
I have your code under a control (button)   in the report that I am trying to export.
I have also closed the report and tried to execute the code from another form.

In each case, I receive the following error

run-time error '2501'
The OutputTo action was cancelled.
peispudAuthor Commented:
Your code was correct.

I was able to fix it by changing the     Const outputToPathFile As String = "C:\YourReport.pdf" to another location on my hard drive.  Must have something to do with privileges.

Thank you!
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Microsoft Access

From novice to tech pro — start learning today.