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
| Option Compare Database
Option Explicit
' Ne pas oublier de cocher la bibliothèqe PDFCreator
' dans le menu Outils / Références de Visual Basic Editor
' API Windows pour faire une temporisation en millisecondes
Private Declare Sub Sleep Lib "kernel32" (ByVal dwMilliseconds As Long)
' Constantes pour les temporisations
Private Const maxTime = 10 ' en secondes
Private Const sleepTime = 250 ' en millisecondes
' ---
' IMPRESSION D'UN ETAT EN PDF
' ---
' Entrée : strReportName <- Nom de l'état à imprimer en PDF
' strWhere <- Clause WHERE filtrant l'état (facultatif)
' strPDFName <- Nom du fichier PDF à générer (facultatif)
' strDirectory <- Chemin de stockage du fichier PDF (facultatif)
'
Public Sub SaveAsPDF( _
ByVal strReportName As String, _
Optional ByVal strWhere As String = "", _
Optional ByVal strPDFName As String = "", _
Optional ByVal strDirectory As String = "")
' Quelques variables...
Dim pdfc As PDFCreator.clsPDFCreator
Dim DefaultPrinter As String
Dim c As Long
Dim OutputFilename As String
' Instancier un nouvel objet PDFCreator
Set pdfc = New clsPDFCreator
' Paramétrer l'objet PDFCreator
With pdfc
.cStart "/NoProcessingAtStartup"
.cOption("UseAutosave") = 1
.cOption("UseAutosaveDirectory") = 1
' Chemin de destination
' Par défaut : dossier 'Mes documents' de l'utilisateur
If strDirectory = "" Then
strDirectory = Environ("USERPROFILE") & "\Mes documents\"
End If
.cOption("AutosaveDirectory") = strDirectory
' Nom du fichier PDF à générer
.cOption("AutosaveFilename") = _
IIf(strPDFName = "", strReportName, strPDFName)
' Format de sauvegarde (0 = PDF)
.cOption("AutosaveFormat") = 0
' Mémoriser l'imprimante par défaut
' et définir PDFCreator à la place
DefaultPrinter = .cDefaultPrinter
.cDefaultPrinter = "PDFCreator"
.cClearCache
' Imprimer l'état
DoCmd.OpenReport strReportName, acViewNormal, , strWhere
.cPrinterStop = False
End With
' Temporisation
c = 0
Do While (pdfc.cOutputFilename = "") And (c < (maxTime * 1000 / sleepTime))
c = c + 1
Sleep 200
Loop
' Nom du fichier PDF produit
OutputFilename = pdfc.cOutputFilename
' Réinstaller l'imprimante d'origine
With pdfc
.cDefaultPrinter = DefaultPrinter
Sleep 200
.cClose
End With
' Attendre jusqu'à ce que PDFCreator soit supprimé de la mémoire
Sleep 2000
' Vérifier si le fichier a été créé
If OutputFilename = "" Then
MsgBox "Création du fichier PDF." & vbCrLf & vbCrLf & _
"Une erreur s'est produite : temps écoulé !", vbExclamation + vbSystemModal
End If
End Sub |
Partager