View Full Version : Auto-naming PDF Document


gcomp
08-13-2010, 10:24 AM
I have set up a macro that among other things outputs a report into PDF format. I set it up to automatically go to the correct directory where I could also put in a file name. Is it possible to have the file named automatically from a field in the report? Specifically I want the PDF file to be named the purchase order number which is one field in the report.

pbaldy
08-13-2010, 11:15 AM
You'd have to get the field value somehow, but this type of thing (this is in 2007):

DoCmd.OutputTo acOutputReport, "rptBooking", acFormatPDF, "C:\Whatever\" & strFileName & ".pdf"

Where strFileName is a string containing your desired name. That could come from a form if appropriate, or a DLookup getting the value from the source query of the report.

gcomp
08-13-2010, 11:38 AM
I am using Access 2007, but I'm not quite sure how to do the string. The query name is POQuery and the field with the number I want as the pdf document name is Expr1. Can you help me out?

pbaldy
08-13-2010, 12:26 PM
Try

Dim strFileName As String
strFileName = DLookup("Expr1", "POQuery")
DoCmd.OutputTo acOutputReport, "rptBooking", acFormatPDF, "C:\Whatever\" & strFileName & ".pdf"

gcomp
08-13-2010, 12:36 PM
I guess I'm having an "idiot" day. I trying to enter your suggestions into my macro where the action is to output to a pdf file. It asks for output filename. This is where I'm entering your suggestions. Is this the proper way or am I way off?

pbaldy
08-13-2010, 02:02 PM
What I posted was VBA code, not a macro. If you're unfamiliar with code, this should help:

http://www.baldyweb.com/FirstVBA.htm