1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104
| Sub PrintToPDFCreator(sPDFFullPath As String, _
reportToPrint As Report, _
Optional sOwnerPassword As String, _
Optional sUserPassword As String, _
Optional bOpenViewer As String, _
Optional bAllowCopy As Boolean, _
Optional bAllowPrint As Boolean, _
Optional bAllowEdit As Boolean)
Dim PDF As New PdfCreatorObj
Dim PDFdevices As PDFCreator_COM.Printers
Dim DefaultPrinterName, Prt As Printer
Dim PDFprinterName As String
Dim PDFCreatorQueue As PDFCreator_COM.Queue
Dim pdfjob As PDFCreator_COM.PrintJob
'On Error GoTo Err_Infos
'Keep the current printer
DefaultPrinterName = Printer.DeviceName
'Get the name of the PDF printer
Set PDFdevices = PDF.GetPDFCreatorPrinters
PDFprinterName = PDFdevices.GetPrinterByIndex(0)
'Direct Printer to PDFcreator
For Each Prt In Printers
If Prt.DeviceName = PDFprinterName Then
Set Printer = Prt
Exit For
End If
Next
Set PDFCreatorQueue = New PDFCreator_COM.Queue
PDFCreatorQueue.Initialize
DoCmd.PrintOut 'Imprimer document actif
If Not PDFCreatorQueue.WaitForJob(10) Then
MsgBox "Impossible de joindre la file d'attente - 10sec. ", , TITLE_POPUP_ERROR
GoTo Err_Infos
End If
Set pdfjob = PDFCreatorQueue.NextJob
With pdfjob
.SetProfileByGuid ("DefaultGuid")
' Set up the pdf security using the SetProfileSetting method of the job object.
'-------------------------------------------------------------------------------
'Since we want to make our pdf more safe, we have to enable the security action first
.SetProfileSetting "PdfSettings.Security.Enabled", "true"
' We set up the encryption level to medium
.SetProfileSetting "PdfSettings.Security.EncryptionLevel", "Rc128Bit"
' Notice that in order to have a user password we have also to set the owner password
' and additionally enable the RequireUserPassword property
.SetProfileSetting "PdfSettings.Security.OwnerPassword", sOwnerPassword
' Require a user password to be able to view the PDF
.SetProfileSetting "PdfSettings.Security.RequireUserPassword", "true"
' Now everyone who wants to open the converted file has to know the security password "myPassword"
.SetProfileSetting "PdfSettings.Security.UserPassword", sUserPassword
' Set Security options
.SetProfileSetting "PdfSettings.Security.AllowToCopyContent", IIf(bAllowCopy, "True", "False")
.SetProfileSetting "PdfSettings.Security.AllowPrinting", IIf(bAllowPrint, "True", "False")
.SetProfileSetting "PdfSettings.Security.AllowToEditTheDocument", IIf(bAllowEdit, "True", "False")
' Setup main option
'-------------------------------------------------------------------------------
.SetProfileSetting "OpenViewer", IIf(bOpenViewer, "True", "False")
.ConvertTo sPDFFullPath
End With
Do Until pdfjob.IsFinished
DoEvents
Loop
Set pdfjob = Nothing
PDFCreatorQueue.ReleaseCom
'Restore Printer to the old printer
For Each Prt In Printers
If Prt.DeviceName = DefaultPrinterName Then
Set Printer = Prt
Exit For
End If
Next
Exit_currentFunction:
Set PDFCreatorQueue = Nothing
Exit Sub
Err_Infos:
MsgBox "Erreur #" & Err.Number & " : " & vbCr & vbCr & _
"Unable to initialize PDFCreator." & vbCr & vbCr & _
"This may be an indication that the PDF application has become corrupted, " & _
"or its spooler blocked by AV software." & vbCr & vbCr & _
"Re-installing PDF Creator may restore normal working."
Resume Exit_currentFunction
End Sub |
Partager