' 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