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 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166
|
Option Compare Database
Option Explicit
' Ce code permet d'imprimer des documents avec PDFCreator directement en VBA
' /!\ Ajouter la référence "PDFCreator"
' Un seul document : ConversionPDF
' Plusieurs documents : DemarrerFusionPDF, ConversionFusionPDF et TerminerFusionPDF
' Déclaration des APIs
Private Declare Function GetTickCount Lib "kernel32" () As Long
' Déclaration du composant principal
Private WithEvents PDFCreator1 As PDFCreator.ClsPDFCreator
' Déclaration de la classe de gestion des erreurs
Private PDFCreatorErreur As clsPDFCreatorError
' Déclaration de la classe de paramétrage
Private PDFCreatorOptions As clsPDFCreatorOptions
' Déclaration des variables globales
Private Imprimante As String
Private InitialisationOK As Boolean
Private Compteur As Integer
Private Sub Class_Initialize()
Set PDFCreator1 = New ClsPDFCreator
Set PDFCreatorErreur = New clsPDFCreatorError
InitialisationOK = False
With PDFCreator1
.cVisible = True
' Pas de traitement au démarrage
If .cStart("/NoProcessingAtStartup") = False Then
MsgBox "Impossible d'initialiser PDFCreator !", vbCritical + vbOKOnly, "Erreur"
Exit Sub
End If
Set PDFCreatorOptions = .cOptions
.cClearCache
' On sauvegarde l'imprimante par défaut
Imprimante = .cDefaultPrinter
End With
InitialisationOK = True
End Sub
Public Sub ConversionPDF(FichierEntreeAvecChemin As String, RepertoireSortie As String, FichierSortieSansExtension As String)
With PDFCreatorOptions
' Répertoire du fichier de sortie
.AutosaveDirectory = RepertoireSortie
' Nom du fichier de sortie
.AutosaveFilename = FichierSortieSansExtension
' Format du fichier de sortie (PDF)
.AutosaveFormat = 0
' Sauvegarde automatique
.UseAutosave = 1
.UseAutosaveDirectory = 1
End With
Set PDFCreator1.cOptions = PDFCreatorOptions
PDFCreator1.cDefaultPrinter = "PDFCreator"
PDFCreator1.cPrintFile FichierEntreeAvecChemin
PDFCreator1.cPrinterStop = False
While PDFCreator1.cPrinterStop = False
DoEvents
Wend
End Sub
Public Sub DemarrerFusionPDF(RepertoireSortie As String, FichierSortieSansExtension As String)
With PDFCreatorOptions
' Répertoire du fichier de sortie
.AutosaveDirectory = RepertoireSortie
' Nom du fichier de sortie
.AutosaveFilename = FichierSortieSansExtension
' Format du fichier de sortie (PDF)
.AutosaveFormat = 0
' Sauvegarde automatique
.UseAutosave = 1
.UseAutosaveDirectory = 1
End With
Compteur = 0
Set PDFCreator1.cOptions = PDFCreatorOptions
PDFCreator1.cDefaultPrinter = "PDFCreator"
End Sub
Public Sub ConversionFusionPDF(FichierEntreeAvecChemin As String)
' On ajoute le document à la file d'attente
PDFCreator1.cPrintFile FichierEntreeAvecChemin
Compteur = Compteur + 1
' Par sécurité
SleepDoEvents 1000
End Sub
Public Sub TerminerFusionPDF()
' On attend que tous les documents soient prêts
While PDFCreator1.cCountOfPrintjobs <> Compteur
DoEvents
Wend
' On demande la fusion de tous les documents qui se trouvent dans la file d'attente
PDFCreator1.cCombineAll
' Par sécurité
SleepDoEvents 1000
' On lance la fusion
PDFCreator1.cPrinterStop = False
' On attend que la fusion soit terminée
While PDFCreator1.cCountOfPrintjobs <> 0
DoEvents
Wend
End Sub
Private Sub PDFCreator1_eReady()
' L'impression est terminée et l'imprimante est libre !
PDFCreator1.cPrinterStop = True
End Sub
Private Sub PDFCreator1_eError()
Set PDFCreatorErreur = PDFCreator1.cError
' On affiche l'erreur
MsgBox "N°" & Trim(Str(PDFCreatorErreur.Number)) & vbCrLf & "Message : " & PDFCreatorErreur.Description, vbCritical + vbOKOnly, "Erreur"
' On restaure l'imprimante par defaut
PDFCreator1.cDefaultPrinter = Imprimante
End Sub
Private Sub Class_Terminate()
' On restaure l'imprimante par défaut
PDFCreator1.cDefaultPrinter = Imprimante
If InitialisationOK = True Then
DoEvents
PDFCreator1.cClose
End If
Set PDFCreator1 = Nothing
Set PDFCreatorErreur = Nothing
Set PDFCreatorOptions = Nothing
End Sub
Private Sub SleepDoEvents(ByVal intervalle As Long)
Dim debut As Long
debut = GetTickCount
While debut + intervalle > GetTickCount
DoEvents
Wend
End Sub |
Partager