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
| Public Sub ImprimePDF(PdfName As String, PDFLocation As String)
Dim PDFCreator1 As PDFCreator.clsPDFCreator ' Objet PDF pour PDFCreator V1.7.3 maximum ...
Dim DefaultPrinter As String ' Imprimante par Défaut (mémorisation)
Dim C As Long ' compteur Temporisation
Dim OutputFilename As String ' Nom du Fichier Généré
Set PDFCreator1 = New clsPDFCreator
With PDFCreator1
.cStart "/NoProcessingAtStartup"
.cOption("UseAutosave") = 1
.cOption("UseAutosaveDirectory") = 1
.cOption("AutosaveDirectory") = PDFLocation ' Répertoire de stockage du Fichier PDF généré
Debug.Print PdfName ' Remplace par des _ les caractères interdits
.cOption("AutosaveFilename") = _
PdfName & "-" & ActiveSheet.Name ' Nom de Fichier = <nom du Fichier>-<nom de l'Onglet>
.cOption("AutosaveFormat") = 0 ' 0 = PDF
DefaultPrinter = .cDefaultPrinter ' Mémorise l'Imprimante pas défaut
.cDefaultPrinter = "PDFCreator" ' écrase par PDFCreator
.cClearCache
End With
ActiveSheet.PrintOut copies:=1, ActivePrinter:="PDFCreator"
Do Until PDFCreator1.cCountOfPrintjobs = 1 ' Attend la Fin du travail pour quitter
DoEvents
Sleep 1000
Loop
Sleep 1000
PDFCreator1.cPrinterStop = False
C = 0 ' Attend la Fin d'Ecriture
Do While (PDFCreator1.cOutputFilename = "") And (C < 50) ' au besoin 50x200ms (1 sec)
C = C + 1
Sleep 200
Loop
OutputFilename = PDFCreator1.cOutputFilename ' Récupère le nom du Fichier Généré
With PDFCreator1
.cDefaultPrinter = DefaultPrinter ' Réattribue l'Imprimante initiale
Sleep 200 ' Tempo de prise en compte avant fermeture
.cClose
End With
Sleep 2000 ' Tempo 2 sec permettant d'assurer la libération de PDFCreator de la Mémoire
If OutputFilename = "" Then
MsgBox "Création Fichier pdf." & vbCrLf & vbCrLf & _
"Une Erreur s'est produite: Délai dépassé!", vbExclamation + vbSystemModal
End If
End Sub |
Partager