Jim Dettman
jimdettman at verizon.net
Wed Nov 17 15:13:33 CST 2010
You can use something like Ghost Script on Post Script printer output to
translate it to a PDF. This is done via a batch file which you would call
with Shell(). Would look something like the procedure below.
Lebans solution avoids the printer setup issues, but note that it outputs
the report to snapshot format first, then translates that.
Jim.
Function RunReportAsPDF(strReportName As String) As String
' Prints a report to disk
' and then converts to a PDF file with Ghost Script.
' Returns name of file if sucessfull
' empty string ("") if not.
' 1.0 - 03/05/05 - JRD - Inital write
' 1.1 - 06/08/06 - JRD - Made name of batch file unique to avoid
collisions between programs
' 1.2 - 09/15/09 - JRD - Removed hardcoded file number
Const RoutineName = "RunReportAsPDF"
Const Version = "1.2"
Dim strPDFPrinter As String
Dim strPDFProgPath As String
Dim strPDFFileName As String
Dim strPDFScriptFile As String
Dim strPRNFile As String
Dim intFileNum As Integer
Dim lngHWnd As Long
10 On Error GoTo RunReportAsPDF_Error
20 RunReportAsPDF = ""
30 strPDFPrinter = GetAppConfigValue(AppShortName(), "PDFPrinter",
".", ".")
40 strPDFProgPath = GetAppConfigValue(AppShortName(), "PDFProgPath",
".", ".")
50 strPRNFile = GetAppConfigValue(AppShortName(), "PDFPrnFile ", ".",
".")
' Kill the printer output file if it already exists.
60 If (Dir(strPRNFile) & "" <> "") Then
70 On Error Resume Next
80 Kill strPRNFile
90 On Error GoTo RunReportAsPDF_Error
100 End If
' JRD *** Need to lock printer - printer output will spool to same
file
' if another program comes along and spools before we convert to
PDF and
' we will end up with a mess.
' Now run the report
110 If SetDefaultPrinter(strPDFPrinter) = True Then
120 DoCmd.OpenReport strReportName, acViewNormal
' Wait until the report completes
130 Do While Dir(strPRNFile) & "" = ""
140 DoEvents
150 Loop
160 Call ResetDefaultPrinter(0)
'Generate file name to use for PDF output and script file.
170 strPDFFileName = GetTempFileName_TSB(AppShortName())
180 strPDFFileName = Replace(strPDFFileName, ".tmp", ".pdf")
190 strPDFScriptFile = strPDFProgPath & AppShortName() &
"_MKPDF.bat"
' Change printer driver output into a PDF
' Script the batch file
200 intFileNum = FreeFile
210 Open strPDFScriptFile For Output As #intFileNum
220 Print #intFileNum, "" & Left(strPDFProgPath, 2)
230 Print #intFileNum, "" & "CD " & Chr$(34) & Mid(strPDFProgPath,
3) & Chr$(34)
240 Print #intFileNum, "" & Chr$(34) & strPDFScriptFile & Chr$(34)
& " " & strPRNFile & " /D /V1.4 /O" & strPDFFileName
250 Close #intFileNum
' Execute batch file and wait till done.
260 lngHWnd = Shell(Chr$(34) & strPDFScriptFile & Chr$(34),
vbMinimizedNoFocus)
270 WaitWhileRunning (lngHWnd)
280 RunReportAsPDF = strPDFFileName
290 Else
300 gstrMBTitle = "Printer not installed."
310 gstrMBMsg = "The printer " & strPDFPrinter & " is not
installed."
320 gstrMBMsg = gstrMBMsg & vbCrLf & "Please correct."
330 gintMBDef = vbCritical + vbOKOnly
340 gintMBBeep = True
350 gintMBLog = True
360 Call DisplayMsgBox
370 RunReportAsPDF = ""
380 End If
RunReportAsPDF_Exit:
390 On Error Resume Next
400 Close #intFileNum
410 Exit Function
RunReportAsPDF_Error:
420 UnexpectedError ModuleName, RoutineName, Version, Err.Number,
Err.Description, Err.Source, VBA.Erl
430 RunReportAsPDF = ""
440 Resume RunReportAsPDF_Exit
End Function
-----Original Message-----
From: accessd-bounces at databaseadvisors.com
[mailto:accessd-bounces at databaseadvisors.com] On Behalf Of jwcolby
Sent: Wednesday, November 17, 2010 03:18 PM
To: Access Developers discussion and problem solving
Subject: [AccessD] Automating pdf printer
A client wants to print stuff to a pdf and send the email. I would create a
report and send it to
the printer, then pick up the resulting file and send an email.
I know how to do everything except the pdf part in the middle.
Is anyone doing this?
--
John W. Colby
www.ColbyConsulting.com
--
AccessD mailing list
AccessD at databaseadvisors.com
http://databaseadvisors.com/mailman/listinfo/accessd
Website: http://www.databaseadvisors.com