Private Sub cmdPrintEmp_Click()
Dim strSave As String
strSave = "EmployeeList_" & Format(Date, "yyyymmdd") & ".PDF"
If PrintReportToPDF("rpt_Employee", strSave) = True Then
MsgBox "The report has been printed as " & vbCrLf & vbCrLf & _
Replace(strSave, "\\", "\")
Else
MsgBox "The report FAILED to print as a PDF file!", vbCritical, "PDF Failed"
End If
End Sub
Public Function PrintReportToPDF(strReport As String, strSave As String) As Boolean
On Error GoTo ErrHandler
WriteRegistryEntry strSave
Set Application.Printer = Application.Printers("Acrobat PDFWriter")
DoCmd.OpenReport strReport, acViewNormal
Application.Printer = Nothing
PrintReportToPDF = True
ExitHere:
Exit Function
ErrHandler:
MsgBox Err.Description
Resume ExitHere
End Function
Public Function WriteRegistryEntry(strPDF As String)
Dim strPath As String
Dim x
strPath = Left(CurrentDb.Name, InStrRev(CurrentDb.Name, "\", , vbTextCompare))
If Dir(strPath & "Reports\", vbDirectory) = "" Then
MkDir strPath & "Reports\"
End If
strPDF = strPath & "Reports\" & strPDF
strPDF = Replace(strPDF, "\", "\\")
On Error Resume Next
Kill strPath & "CreatePDF.reg"
On Error GoTo ErrHandler
Open strPath & "CreatePDF.reg" For Append As #1
Print #1, "Windows Registry Editor Version 5.00"
Print #1, ""
Print #1, "[HKEY_CURRENT_USER\Software\Adobe\Acrobat PDFWriter]"
Print #1, """PDFFilename""=" & Chr(34) & strPDF & Chr(34)
Close #1
x = Shell("regedit.exe /s " & strPath & "CreatePDF.reg", vbHide)
ExitHere:
On Error Resume Next
Close #1
Kill strPath & "CreatePDF.reg"
Exit Function
ErrHandler:
MsgBox Err.Description
Resume ExitHere
End Function
|