Impression PDF avec PDF Creator
Bonjour,
J'ai créé un fichier excel pour mes tests en pièce jointe.
La génération du fichier PDF est niquel mais là ou je bloque c'est sur la partie dossier d'enregistrement.
Actuellement le fichier s'enregistre dans le dossier ou le fichier excel est enregistrer.
je voudrai qu'il s'enregistre dans le dossier c:\pdf\sheets("Feuil1").range("A2).value \ sheets("Feuil1").range("A3).value
et que si ce dossier n'existe pas qu'il soit créé.
exemple c:\PDF\2013\juillet\
Voici mon code :
Sub PrintToPDF()
Dim pdfjob As PDFCreator.clsPDFCreator
Dim sPDFName As String
Dim sPDFPath As String
'Définition du nom de fichier à exporter
sPDFName = "Fichier PDF du " & Range("A1").Value & ".pdf"
'Chemin du dossier pour l'enregistrement
sPDFPath = ActiveWorkbook.Path & Application.PathSeparator
'Vérifier si la feuille est vide ou non
If IsEmpty(ActiveSheet.UsedRange) Then Exit Sub
Set pdfjob = New PDFCreator.clsPDFCreator
With pdfjob
If .cStart("/NoProcessingAtStartup") = False Then
MsgBox "Can't initialize PDFCreator.", vbCritical + _
vbOKOnly, "PrtPDFCreator"
Exit Sub
End If
.cOption("UseAutosave") = 1
.cOption("UseAutosaveDirectory") = 1
.cOption("AutosaveDirectory") = sPDFPath
.cOption("AutosaveFilename") = sPDFName
.cOption("AutosaveFormat") = 0 ' 0 = PDF
.cClearCache
End With
'Imprime le document en PDF
ActiveSheet.PrintOut copies:=1, ActivePrinter:="PDFCreator"
'Attendre jusqu'à ce que l'impression soit dans la file d'attente
Do Until pdfjob.cCountOfPrintjobs = 1
DoEvents
Loop
pdfjob.cPrinterStop = False
'Attendre que PDF Creator libère les objets
Do Until pdfjob.cCountOfPrintjobs = 0
DoEvents
Loop
pdfjob.cClose
Set pdfjob = Nothing
End Sub
et mon fichier :
Même dans l'absolu mettre en A4 par exemple c:\pdf pour pouvoir choisir l'arborescence de base si c'est possible.
Re,
en fouillant un peu je suis arrivée à faire ceci (le programme fonctionne en l'état) :
Sub PrintToPDF()
Dim pdfjob As PDFCreator.clsPDFCreator
Dim sPDFName As String
Dim sPDFPath As String
'Définition du nom de fichier à exporter
sPDFName = "Fichier PDF du " & [A1].Value & ".pdf"
'Test si dossier existe, sion on le créer via la fonction RépertoireExiste
Call RépertoireExiste([A4])
Call RépertoireExiste([A4] & "\" & [A2])
Call RépertoireExiste([A4] & "\" & [A2] & "\" & [A3])
'Chemin du dossier pour l'enregistrement
sPDFPath = [A4] & "\" & [A2] & "\" & [A3] & "\"
'Vérifier si la feuille est vide ou non
If IsEmpty(ActiveSheet.UsedRange) Then Exit Sub
Set pdfjob = New PDFCreator.clsPDFCreator
With pdfjob
If .cStart("/NoProcessingAtStartup") = False Then
MsgBox "Can't initialize PDFCreator.", vbCritical + _
vbOKOnly, "PrtPDFCreator"
Exit Sub
End If
.cOption("UseAutosave") = 1
.cOption("UseAutosaveDirectory") = 1
.cOption("AutosaveDirectory") = sPDFPath
.cOption("AutosaveFilename") = sPDFName
.cOption("AutosaveFormat") = 0 ' 0 = PDF
.cClearCache
End With
'Imprime le document en PDF
ActiveSheet.PrintOut copies:=1, ActivePrinter:="PDFCreator"
'Attendre jusqu'à ce que l'impression soit dans la file d'attente
Do Until pdfjob.cCountOfPrintjobs = 1
DoEvents
Loop
pdfjob.cPrinterStop = False
'Attendre que PDF Creator libère les objets
Do Until pdfjob.cCountOfPrintjobs = 0
DoEvents
Loop
pdfjob.cClose
Set pdfjob = Nothing
End Sub
Function RépertoireExiste(Chemin As String) As Boolean
On Error Resume Next
RépertoireExiste = GetAttr(Chemin) And vbDirectory
If RépertoireExiste = True Then
Exit Function
Else
MkDir (Chemin)
End If
End Function
Quelqu'un pourrait il vérifier mon code pour voir si il est correct et bien optimisé ?