PDF Conversion Issues

Fix Microsoft Access VBA Code That Exports a Report to PDF

Natalie TaylorNatalie Taylor Nov 25, 2026 868 views

Question details

Fix VBA code that saves a Microsoft Access report as a PDF.

Fix Microsoft Access VBA Code That Exports a Report to PDF
Product
Microsoft Access
Device & OS
not provided
Scenario
A user wants to build a dated filename and export an Access report to PDF using VBA.
Observed behavior
The code can fail because of duplicate variable declarations, invalid filename characters, or incorrect string concatenation.
Before you start

Open the Access database and identify the exact report name and a writable folder for the PDF. Check the export procedure for duplicate declarations and any filename components containing reserved Windows characters.

Solution 1Recommended

Correct the VBA declaration and PDF export filename

Use this approach to fix the procedure and export the report with a valid dated filename.

1
Open the VBA procedure

In Access, press Alt+F11 to open the VBA editor, then open the module containing the report export procedure.

2
Remove duplicate declarations

Find each declaration of strFileName in the procedure and keep only one, such as Dim strFileName As String.

3
Correct the export code

Replace or adapt the procedure with the example below. Change Sales Report to the exact name of your report; the PDF will be saved beside the database file. Public Sub ExportReportToPDF() Dim strFileName As String Const reportName As String = "Sales Report" strFileName = CurrentProject.Path & "\Sales_Report_" & Format(Date, "yyyy-mm-dd") & ".pdf" DoCmd.OpenReport reportName, acViewPreview DoCmd.OutputTo acOutputReport, reportName, acFormatPDF, strFileName, False DoCmd.Close acReport, reportName, acSaveNo End Sub

4
Compile and run the procedure

In the VBA editor, select Debug > Compile, correct any remaining errors, and run the procedure. Open the database folder and check the exported PDF.

Correct the VBA declaration and PDF export filename
Check dynamic filename values: Remove or replace Windows-reserved filename characters such as \ / : * ? " < > and | from any values added to the filename. If using PdfSaveDate, concatenate it with & and format it without slashes or colons.
Free Microsoft Office alternative

Export Office documents to PDF with WPS Office

WPS Office is a free, lightweight alternative for working with familiar Office documents and exporting supported files to PDF. Its familiar interface and format compatibility make moving everyday documents straightforward. It does not fix Access VBA code or run Access reports.

Export supported documents and spreadsheets to PDFCompatible with Microsoft Office formats including DOCX, XLSX, and PPTXFamiliar interface for everyday document editingFree and lightweight for easy migration
microsoft office alternative - wps office

Frequently Asked Questions

Why does Access report a duplicate declaration for strFileName?

The variable is declared more than once in the same procedure. Open the procedure in the VBA editor and remove the extra Dim strFileName declaration.

Which characters are invalid in a Windows PDF filename?

Windows filenames cannot contain \ / : * ? " < > or |. Remove or replace these characters in any report title or other value used to build the filename.

How do I add a date to the exported PDF filename?

Use Format(Date, "yyyy-mm-dd") and join it to the filename with the & operator. Avoid date formats containing slashes or colons.

What does DoCmd.OutputTo do in this procedure?

DoCmd.OutputTo exports the named Access report. Use acOutputReport for the object type, acFormatPDF for the format, and a full valid path for the output filename.