Public Sub SaveToPDF(Chemin As String, nomfichier As String, Feuille As String) Dim Temp As Variant Temp = Worksheets(Feuille).PrintOut(, , 1, , "PDFCreator", True, True, Chemin + nomfichier + ".ps") ' Les Chr(34) sont importants. Ils définissent les guillemets anglais double. Shell (Chr(34) + "C:\Program Files\PDFCreator\PDFcreator.exe" + Chr(34) + "-IF" + Chr(34) + Chemin + nomfichier + ".ps" + Chr(34) + "-OF" + Chr(34) + Chemin + nomfichier + ".pdf" + Chr(34)) Application.Wait (Now + TimeValue("0:00:02")) Kill Chemin + nomfichier + ".ps" End Sub
Application.Wait (Now + TimeValue("0:00:02"))
Vous n’avez pas trouvé la réponse que vous recherchez ?
Posez votre questionPrivate Sub CommandButton1_Click() OngletsToPrint.Add "budget" OngletsToPrint.Add "dépenses" OngletsToPrint.Add "synthèse" Call ImprimeTousPDF("essai", "C:\Documents and Settings\mondossier") End Sub
'=============================================================================== ' SOURCE 'http://www.hastursoft.com/pdf/generer-un-pdf-par-macro-vba-sous-excel-t23.html '=============================================================================== Option Explicit Public OngletsToPrint As New Collection Private Declare Sub Sleep Lib "kernel32.dll" (ByVal dwMilliseconds As Long) Public Sub ImprimeTousPDF(PDFName As String, PDFLocation As String) Dim PDFCreator1 As PDFCreator.clsPDFCreator 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é Dim i As Integer ' compteur d'onglets Dim x As Variant 'rajouté par moi ( permet de voir si l'onglet appartient à la liste à imprimer) Dim idx As Integer Dim nbonglets As Integer nbonglets = OngletsToPrint.Count 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 ' Nom de Fichier <nom du Fichier> .cOption("AutosaveFormat") 0 ' 0 PDF DefaultPrinter = .cDefaultPrinter ' Mémorise l'Imprimante pas défaut .cDefaultPrinter = "PDFCreator" ' écrase par PDFCreator .cClearCache End With For i = 1 To Application.Sheets.Count For Each x In OngletsToPrint If x = Application.Sheets(i).Name Then Application.Sheets(i).PrintOut Copies:=1, ActivePrinter:="PDFCreator" idx = indexOf(x, OngletsToPrint) OngletsToPrint.remove idx Exit For End If Next Next i Do Until PDFCreator1.cCountOfPrintjobs = nbonglets ' Attend la Fin du travail pour quitter DoEvents Sleep 1000 Loop Sleep 1000 PDFCreator1.cCombineAll 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 Public Function indexOf(obj As Variant, ByRef col As Collection) As Integer 'retourne l'index de l'objet dans la cpllection Dim x As Variant Dim i As Integer Dim idx As Integer i = 1 For Each x In col If x = obj Then idx = i Exit For Else idx = -1 End If i = i + 1 Next indexOf = idx End Function