Access VBA Generate report as PDF and save in folder

Hi

I want to use Access VBA code to generate a report called "Invoice" and automatically convert it to a PDF and save it in a specified folder with a different name.

What VBA code would I use?
Murray BrownMicrosoft Cloud Azure/Excel Solution DeveloperAsked:
Who is Participating?
 
PatHartmanConnect With a Mentor Commented:
That's a start but you will almost certainly want the file name to be variable and contain the invoice number you generated.  Also, the OutputTo method does not provide a where argument so you need to handle this in your report's query or by using some other method.

Here's an example that formats the file name and writes out multiple reports inside a loop.

    sD = Form_frmMain.txtStartDate
    eD = Form_frmMain.txtEndDate
    rNow = Format(Date, "yyyymmdd")
    tDate = rNow & "_" & Format(sD, "yyyymmdd") & "_" & Format(eD, "yyyymmdd") & "_"
    
    For Each sPD In Me.lstEnrollments.ItemsSelected
        strSPD = Me.lstEnrollments.ItemData(sPD)
        Me.txtWorkingRecord2 = strSPD
        RecCount = RecCount + 1
        Me.txtCounter2 = RecCount
        stDocName = "rptEnrollmentsWithoutDetails"
        FileName = "EnrollmentsWithoutDetails_" & strSPD
        XFile = FileDir & tDate & FileName & " .pdf"
            'DoCmd.OpenReport "rptEnrollmentsWithoutDetails", acViewPreview
            DoCmd.OutputTo acOutputReport, "rptEnrollmentsWithoutDetails", acFormatPDF, XFile, False
            'DoCmd.Close acReport, "rptEnrollmentsWithoutDetails", acSaveNo             
    Next sPD

Open in new window

0
 
Rgonzo1971Connect With a Mentor Commented:
Hi,

pls try
DoCmd.OutputTo acOutputReport, "Invoice", _ 
acFormatPDF,"C:\Users\desktop\PDFs\myInvoiceReport.pdf"

Open in new window

Regards
0
 
Murray BrownMicrosoft Cloud Azure/Excel Solution DeveloperAuthor Commented:
Thanks very much
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.