export individual pdfs

stars14

New member
Local time
Today, 05:14
Joined
Jun 10, 2005
Messages
9
I have a 1 page report that is tied to 5,000 records. I have a need to export 5,000 individual pdf files. I can imagine the code for this cycling through each record and exporting pdf but dont know how to write it. Anyone done something like this? You may be helping the City of San Diego in many ways. Much appreciated!

Mike
 
If you have access 2007 (which I don't), you would have the ability to generate report into PDF. However, previous version of access would require an external library which you need to purchase.

Please be carefull in choosing to buy products, as what you need is a library to be used in your code, not an "Add-on". Most pdf products are designed as an add on which give you a convenient user interface. You would want a library in which you can use as function or procedure in your code.

The one I use in workplace is "ACG PDF and Mail Library" which you can find in htttp:\\www.groupacg.com.

hope that help
 
It's actually really easy to do what you are asking. I will try to explain it concisely here, but if you don't understand it, PM me and I will go more into detail.

I used to use Adobe Acrobat to accomplish this, but that got to be cost prohibitive considering that every one of my users needed an Acrobat license. Instead, I opted for a free PDF writer, (PDF Creator) and performs the job just as well.

Now, the steps you want to take are as follows:

In the Page Setup of the report, click on the Page tab, and select use a specific printer, at which point you will choose your PDF printer. Save the report.

In VBA, create a procedure that will perform the following:

Open a recordset that will hold the Primary Key for each of the 5000 records.
Starting from the first record, loop until recordset.EOF, and at each step in the loop, use VBA to open the report hidden, change the caption of the report to the Primary Key and save and close the report. (the purpose of doing this is so when the report is automatically saved by the PDF writer, it will use the report caption as the name of the file)
Then, simply open the report using Docmd.OpenReport "[Report Name]",,,"[NameOfPKHere]=" & rst!PrimaryKey (you would need quotes if your primary key is text).

That's really all there is in the basic version. Of course you could beef it up a little by adding in code that would automatically relocate and sort the files for you, but that is a little more complicated.

You may need to change some defaults with the PDF writer, such as set it to use AutoSave instead of prompting for a name each time. Also, you would need to set it to use the Report Title (caption) as the default file name. (Adobe PDF Writer, when set to autosave, uses the caption by default)

I hope this helps!
 
Sorry to dig up an old thread, but I am undertaking an almost indentical task.

I have Access 2007, and Adobe Acrobat 8 Pro.

I can make the pdf file save without an prompts, but I am unable to make the Caption of the report set to the ID field of the report. I have tried setting the value on events like Open, Load, Activate etc, but no luck.

Does anyone know a method to get around this problem?
 
i haven't had access to 2007 yet, but I would assume it would work the same way.

The way i've done this in the past is by creating a form with a button to generate the report. this button first opens the report in design view in hidden mode, changes the caption, saves the changes and closes the report. Then you can simply use the docmd.openreport in normal mode which will automatically print the report to the default printer (in your case presumably the PDF printer). Assuming you had a report named sample_report, here would be the code:
Code:
DoCmd.OpenReport "Sample_Report", acViewDesign, , , acHidden
Reports!Sample_Report.Caption =  "SAMPLE" & ID 'or whatever you want to name it
DoCmd.Close acReport, "Sample_Report", acSaveYes
DoCmd.OpenReport "Sample_Report",...
 
Thanks for that one I must of spent about 3 hours pulling my hair out with this issue. I was on the same wave length, but couldnt quite figure it out!
 
Last edited:
my pleasure - good thing i was still subscribed to this thread :)
 
This is great and works excellent;

DoCmd.OpenReport "Sample_Report", acViewDesign, , , acHidden
Reports!Sample_Report.Caption = "SAMPLE" & ID 'or whatever you want to name it
DoCmd.Close acReport, "Sample_Report", acSaveYes
DoCmd.OpenReport "Sample_Report",...

How can I create one pdf for each record?

Is that possible?

Fen How
 
it's actually really easy - look at my original post it describes the method. You would need to use vb to open a recordset with the primary key of each record you want to export to pdf, then loop through that recordset using docmd.openReport whereCondition:="fldID=" & rst!fldID

let me know if this helps you or you need more of an explanation.
 
You need to create a recordset for the list of record you have, and create a loop which performs the action for each record.

Code:
Dim rs As Recordset
Dim RecordSetString As String

RecordSetString = Form_YOURForm.recordsource

Set rs = CurrentDb.OpenRecordset(RecordSetString)

rs.MoveFirst

Do While Not rs.EOF

    Dim stDocName As String
    Dim strDefaultPrinter As String
     
    stDocName = "YOUR REPORT"
    strDefaultPrinter = Application.Printer.DeviceName
     
    Set Application.Printer = Application.Printers("YOUR PDF PRINTER")
     
    DoCmd.OpenReport "YOUR REPORT", acViewDesign, , , acHidden
    "YOUR REPORT".Caption = rs!"YOUR ID FIELD NAME"
    DoCmd.CLOSE acReport, "YOUR REPORT", acSaveYes
     
    DoCmd.OpenReport stDocName, acViewNormal, , "YOUR ID FIELD NAME" = " & rs!"YOUR ID FIELD NAME"
      
    Set Application.Printer = Application.Printers(strDefaultPrinter)

rs.MoveNext

Loop

rs.CLOSE

EDIT: Lol Shudini posted same time as you. Have to thank you once again for that change caption code. :)
 
Last edited:
Ability to Batch Reports into one PDF File

I pm'd Shudini for help figuring out a one-button solution to group individual reports into one PDF file. PDFCreator's COM utilities had some sample code with a reference library that Shuie figured out how to use. The attached db has a module with the original code from PDFCreator as well as one with the code we came up with. Don't forget to add a reference to the PDFCreator library after you import the module into your file. You can create a pdf file with any combination of the 3 sample reports. I hope this is useful to somebody. Thanks to Shuie for working all this out.

p.s. PDFCreator doesn't work fully properly in Vista. They are offering a $150 bounty to anyone who can fix it.
 

Attachments

I found Bullzip to be a good free PDF converter. That might suit your needs.

Runs in Vista.
 
Hi all,

I have PDF Creator and Access 2000.
But I get the compile error: Can't find project or library

PDFCreator1 As PDFCreator.clsPDFCreator

Can some one help?

Tks
 
I'm pretty ignorant about the precise workings of this, but I think you'll get the library if you import my module into your file. Then you could reference it from your code and delete my module. Just a workaround-thought.

I'm pursuing a BullZip solution at the moment. I have it working for one report, but the batching, or merging, piece isn't quite working yet. I'm planning to post a sample file with it once I get it working.

Good luck,
Dudley
 
shudini, sorry to hijack this thread, but it sounds like you know the answer to a question I have posted...

I have an annual valuation report that I need to print to PDF and save in an existing folder. At the moment I have a control button which prints the report. I have the report setup to print to CutePDF so when I hit the button it 'prints' and you get a 'Save As' box to name the file & choose where to put it.

What I want to do is name the file using a field from the report (report date) and then save it in an existing folder. I have a text field for each client which contains the location of their particular folder (which I've made into a hyperlink to get directly to their records outside of Access) so I just need a way of saving to that location and naming the file with a field from the report (report date)

Any help would be much appreciated.
 
Hi,
I'm still trying to put together a "one-button" solution for generating multiple reports and saving them as one PDF file. PDFCreator is buggy in Vista, so Terbs suggested BullZip. Turns out Bullzip is buggy when it comes to appending reports as additional pages in a pdf file. I've attached the sample db with my bullzip code to see if anyone can help figure out where I'm going wrong. I've posted my difficulty on the Bullzip forum as well - seems like I'm not alone - but with no help forthcoming yet. I can't find helpful info about the ghostscript error (1007), either. Has anyone got this sort of issue worked out?
 

Attachments

To anybody interested in this issue: [I'm running Vista Business and Access 2002 SP3]

I think I've got it now. Someone on the PDF Creator forum came up with a solution for running PDF Creator in Vista: http://www.pdfforge.org/node/1058/4110#comment-4110

Having set the PDF Creator executables to run under XP, the code below and in the sample db attached seems to work consistently.

The sleep intervals seem necessary to stay synced with PDFCreator.

I wanted to be sure that my autosave settings didn't hijack PDFCreator, so I came up with that Reset utility to turn off autosave.

Code:
Private Sub cmdPrint_Click()
    PrintRPTToPDFCreator
    ResetPDFCreator 'turn off autosave
End Sub

Code:
' Add a reference to PDFCreator

Private Declare Sub Sleep Lib "kernel32" (ByVal dwMilliseconds As Long)

Private Const maxTime = 10    ' in seconds
Private Const sleepTime = 250 ' in milliseconds

'Public Sub PrintRPTToPDFCreator(RepName As String)
Public Sub PrintRPTToPDFCreator()

    Dim PDFCreator1 As PDFCreator.clsPDFCreator
    Dim DefaultPrinter As String
    Dim c As Long
    Dim OutputFilename As String
    
    Set PDFCreator1 = New clsPDFCreator
 
    With PDFCreator1
      .cStart "/NoProcessingAtStartup", True
      .cOption("UseAutosave") = 1
      .cOption("UseAutosaveDirectory") = 1
      .cOption("AutosaveDirectory") = "<MyFiles>\Reports\"
      .cOption("AutosaveFilename") = "Report_<DateTime>"
      .cOption("AutosaveFormat") = 0     ' 0 = PDF
      .cOption("AutosaveStartStandardProgram") = 1
      
      'these lines hold the identity of the system's default printer so it can be reset
      DefaultPrinter = .cDefaultPrinter
      .cDefaultPrinter = "PDFCreator"
      .cClearCache
      
      .cSaveOptions
      'so i can see what's going on
      .cSaveOptionsToFile ("C:\Users\Hope\Documents\PDFCreator.ini")
 
      'My loop to print all the reports -
      'create a variable 'intRPTCounter' that will keep track of how many reports being generated
    
            Dim counter As Integer
            Dim intRptCounter As Integer
            Dim strItem As String
            Dim strReport As String
            
            counter = 1
            intRptCounter = 0
            
            'Run through the checkboxes to count the Reports to be generated
            For counter = 1 To 3
                
                strItem = "ckItem" & counter
                
                'If Item is checked, then count it
                If Forms!frmRPTS_To_PDF.Controls(strItem) = -1 Then
                    intRptCounter = intRptCounter + 1
                End If
            Next counter
            
            'reset counter for next loop
            counter = 1
            
            'Start looping through the item checkboxes and create reports as indicated
            For counter = 1 To 3
                
                strItem = "ckItem" & counter
                
                'If Item is checked, then create the report
                If Forms!frmRPTS_To_PDF.Controls(strItem) = -1 Then
                    strReport = "rptReport" & counter
                    DoCmd.OpenReport strReport, acViewDesign, , , acHidden
                    Reports(strReport).Caption = strReport  'or whatever you want
                    DoCmd.Close acReport, strReport, acSaveYes
                    DoCmd.OpenReport strReport, acViewNormal
                End If
                
            Next counter
    
    
      'Now you need to wait until all the documents have been spooled to the PDFCreator.
      'To do this, create a loop that waits until the spooled document count = the number of created documents
        
        c = 0
        Do Until (.cCountOfPrintjobs = intRptCounter) And (c < (maxTime * 1000 / sleepTime))
            c = c + 1
            Sleep 200
        Loop
        
        'now add in the code to combine all the documents
        .cCombineAll
        'unstop the printer so it will create the batched report
        .cPrinterStop = False
        'wait until its finished:
        Do Until .cCountOfPrintjobs = 0
          Sleep 200
        Loop
    
    End With

    With PDFCreator1
        'reset default printer
        .cDefaultPrinter = DefaultPrinter
        Sleep 2000
        .cClose
    End With

    Sleep 2000 ' Wait until PDFCreator is removed from memory
    

End Sub

Public Sub ResetPDFCreator()    'turn off autosave
    Dim PDFCreator2 As PDFCreator.clsPDFCreator
    
    Set PDFCreator2 = New clsPDFCreator
 
    With PDFCreator2
      .cStart "/NoProcessingAtStartup", True
      .cOption("UseAutosave") = 0
      .cSaveOptions
      Sleep 2000
      .cClose
    End With
    
    Sleep 2000

End Sub
 

Attachments

dudley, does this sleep interval prevent the PDF Distiller from crashing?

If I try the loop with > 50 records it seems to get to about the 40th when Acrobat just crashes itself..
 

Users who are viewing this thread

Back
Top Bottom