Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations gkittelson on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Save a report in PDF Format 1

Status
Not open for further replies.

Ielamrani

MIS
Jul 7, 2005
183
US
Hi,
I have an access report called "ReportR -MainReport". I would like to create a button in a form, once I click on this button the report will be transfered to a folder in PDF Format.
This is a routine that I do everyday and I am looking to automate it.

Thanks in advance
 
Courtesy of JonoB at Utteraccess.com

Code:
Option Compare Database
Option Explicit

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

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

Public Function PrintPDF(ByVal rptName As String, ByVal sFilterCriteria As String, Optional sAutoSaveDirectory As String, Optional sAutoSaveFileName As String) As Boolean

'initialise
On Error GoTo err_Error
Dim clsPDF As PDFCreator.clsPDFCreator 'Initialise the PDF class
Dim strDefaultPrinter As String
Dim strOutputFileName As String
Dim strSaveDirectory As String
Dim i As Long

'set the success variable to true here but it will be set to
'false if the function fails at any point
PrintPDF = True

Set clsPDF = New clsPDFCreator

With clsPDF
.cStart "/NoProcessingAtStartup"
.cOption("UseAutosave") = 1 '1 = True, 0 = False

If sAutoSaveDirectory = "" Then
.cOption("UseAutosaveDirectory") = 0
Else
.cOption("UseAutosaveDirectory") = 1
.cOption("AutosaveDirectory") = sAutoSaveDirectory
End If

If sAutoSaveFileName = "" Then
.cOption("AutosaveFileName") = rptName
Else
.cOption("AutosaveFileName") = sAutoSaveFileName
End If

.cOption("AutosaveFormat") = 0 '0 = PDF
'strDefaultPrinter = .cDefaultPrinter
'.cDefaultPrinter = "PDFCreator"
.cClearCache
Sleep 1
DoCmd.OpenReport rptName, acViewNormal, , sFilterCriteria
.cPrinterStop = False
End With

i = 0

Do While (clsPDF.cOutputFilename = "") And (i < (maxTime * 1000 / sleepTime))
i = i + 1
Sleep 500
Loop

strOutputFileName = clsPDF.cOutputFilename

With clsPDF
'.cDefaultPrinter = strDefaultPrinter
'Sleep 200
.cClose
End With

If strOutputFileName = "" Then
' MsgBox "Creating pdf file." & vbCrLf & vbCrLf & _
' "An error has occured: Time is up!", vbExclamation + vbSystemModal
PrintPDF = False
Call HandleError("Report " & sAutoSaveFileName & " did not print", "modPDFPrinter_PrintPDF")
End If

'error handler and exit
err_Exit:
Set clsPDF = Nothing
Exit Function
err_Error:
PrintPDF = False
MsgBox Err.Description
Call HandleError(Err.Description, "modPDFPrinter_PrintPDF")
Resume err_Exit
Resume

End Function

There is also another example at:


Sean.
 
Elementary, perhaps, but how do I 'activate' the code?

Thanks.

swtrader
-- The trouble with doing something right the first time is that no one appreciates how hard it was.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top