Enregistrement par VBA en format Pdf
Bonjour !
Voici le code VBA qui présente mon problème :
Code:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20
| Sub Adobe()
' Je cherche a imprimer un fichier Excel en pdf en spécifiant le nom du fichier
'*** Pensez à adapter l'adresse d'impression Adobe à la vôtre !
' Quand j'utilise la commande sans nom ci-apres, cela marche bien...
ActiveWindow.SelectedSheets.PrintOut Copies:=1, ActivePrinter:= _
"Adobe PDF sur Ne04:", Collate:=True
' Quand j'active la commande ci-dessous, la syntaxe bloque
ActiveWindow.SelectedSheets.PrintOut Copies:=1, ActivePrinter:= _
"Adobe PDF sur Ne04:", PrintToFile:="Jules.pdf", Collate:=True
' Quand j'active la commande ci-dessous, ça plante meme si je fais
' ce qui est demande dans les Options d'Adobe...
ActiveWindow.SelectedSheets.PrintOut Copies:=1, ActivePrinter:= _
"Adobe PDF sur Ne04:", Collate:=True, PrToFileName:="Jules.pdf"
End Sub |
Comment faire pour spécifier le chemin et le nom du fichier pdf ?
Merci d'avance !
Deux essais qui avortent...
Bonjour,
Avec la suggestion de "fring" plus celle trouvée sur une autre adresse (listée au début du code VBA), j'ai deux macros VBA qui plantent toutes les deux. Voici leur code :
' http://excelguru.ca/node/22
Code:
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
| Option Explicit
Sub PrintToPDF_Late()
'Author : Ken Puls (www.excelguru.ca)
'Macro Purpose: Print to PDF file using PDFCreator
' (Download from http://sourceforge.net/projects/pdfcreator/)
' Designed for late bind, no references req'd
Dim pdfjob As Object
Dim sPDFName As String
Dim sPDFPath As String
'/// Change the output file name here! ///
sPDFName = "testPDF.pdf"
sPDFPath = ActiveWorkbook.Path & Application.PathSeparator
'Check if worksheet is empty and exit if so
If IsEmpty(ActiveSheet.UsedRange) Then Exit Sub
Set pdfjob = CreateObject("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
'Print the document to PDF
ActiveSheet.PrintOut Copies:=1, ActivePrinter:="PDFCreator"
'Wait until the print job has entered the print queue
Do Until pdfjob.cCountOfPrintjobs = 1
DoEvents
Loop
pdfjob.cPrinterStop = False
'Wait until PDF creator is finished then release the objects
Do Until pdfjob.cCountOfPrintjobs = 0
DoEvents
Loop
pdfjob.cClose
Set pdfjob = Nothing
End Sub
Sub Tst_Adobe_PDF()
Dim sNomFichierPS As String
Dim sNomFichierPDF As String
Dim sNomFichierLOG As String
Dim PDFDist As PdfDistiller
Dim PrinterDefault As String
Dim sUserProfile As String
sUserProfile = Environ("USERPROFILE")
PrinterDefault = Application.ActivePrinter
Application.ActivePrinter = Imprimante_AdobePDF
' Ici le cas d'un PC "Entreprise"
sNomFichierPS = sUserProfile & "\" & "Essai_AdobbePDF.ps"
sNomFichierPDF = sUserProfile & "\" & "Essai_AdobbePDF.pdf"
sNomFichierLOG = sUserProfile & "\" & "Essai_AdobbePDF.log"
ActiveSheet.Range("Zone").PrintOut Copies:=1, Preview:=False, _
ActivePrinter:="Acrobat PDF", PrintToFile:=True, _
Collate:=True, PrToFilename:=sNomFichierPS
Set PDFDist = New PdfDistiller
PDFDist.FileToPDF sNomFichierPS, sNomFichierPDF, ""
Set PDFDist = Nothing
Kill sNomFichierPS
Kill sNomFichierLOG
Application.ActivePrinter = PrinterDefault
End Sub |
Avec la première macro :
Set pdfjob = CreateObject("PDFCreator.clsPDFCreator")
bloque avec le message "Erreur 429 - Un composant ActiveX ne peut pas créer d'objet".
Avec la seconde macro :
Dim PDFDist As PdfDistiller
bloque avec le message : "Erreur de compilation - Type non défini par l'utilisateur".