Imprimer PDF

Dolichotis

XLDnaute Occasionnel
Bonjour,

J'ai trouvé un code qui permet d'avoir un document PDF d'une sélection d'une feuille Excel.
Je l'ai adapté pour mon usage personnage, ça fonctionnait parfaitement mais maintenant ça me créé un document pdf vide !!

Ai-je changé une chose qui ne fallait pas ? J'ai regardé le code sous tous les angles, je n'ai pas trouvé !

Code:
    'imprimer un document pdf
    Dim PDFdoc As PDFCreator.clsPDFCreator
    Set PDFdoc = New PDFCreator.clsPDFCreator

    Dim sPDFName As String, sPDFPath As String
    Dim RetVal As Variant, Area As String
    
    ChArch = "" & chemin & "\Archives\" & Application.PathSeparator
    NameArch = "" & TypOuvr & "ref" & Ref & "VISA.pdf" 'nom du PDF
    Area = "D33:F44"
    
    With PDFdoc
            If .cStart("/NoProcessingAtStartup") = False Then
                MsgBox "Can't initialize PDFCreator.", vbCritical + vbOKOnly, "PrtPDFCreator"
                RetVal = Shell("Taskkill /IM PDFCreator.exe /F", 0)
            End If
            .cOption("UseAutosave") = 1
            .cOption("UseAutosaveDirectory") = 1
            .cOption("AutosaveDirectory") = ChArch
            .cOption("AutosaveFilename") = NameArch
            .cOption("AutosaveFormat") = 0    ' 0 = PDF
            .cClearCache
    End With

    With wbEx.Worksheets(1).PageSetup 'tout sur une feuille
        .Zoom = False
        .FitToPagesWide = 1
        .FitToPagesTall = 1
    End With
    
    wbEx.Worksheets(1).Activate
    wbEx.Worksheets(1).Range(Area).Select
    Selection.PrintOut ActivePrinter:="PDFCreator sur Ne00:" 'choix de la zone d'impression

    Do Until PDFdoc.cCountOfPrintjobs = 1
        DoEvents
    Loop
    
    PDFdoc.cPrinterStop = False
    'Wait until PDF creator is finished then release the objects
    Do Until PDFdoc.cCountOfPrintjobs = 0
        DoEvents
    Loop

    PDFdoc.cClose
    Set PDFdoc = Nothing 'libérer l'objet

Si vous pouviez regarder mon code et me dire ce qui ne va pas avec, je vous remercie !!
 

Dolichotis

XLDnaute Occasionnel
Re : Imprimer PDF

Re

J'ai retrouvé le code sur excel-download : ICI

Je l'ai arrangé pour qu'il imprime une sélection de cellules voulues.
Code:
Sub PrintPDF()

    Dim pdfjob As PDFCreator.clsPDFCreator
    Dim sPDFName As String, sPDFPath As String
   
    sPDFName = "VISA.pdf"
    sPDFPath = ActiveWorkbook.Path & Application.PathSeparator
    
    'Check if worksheet is empty and exit if so
    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"
            RetVal = Shell("Taskkill /IM PDFCreator.exe /F", 0)
            Exit Sub
        End If
        .cOption("UseAutosave") = 1
        .cOption("UseAutosaveDirectory") = 1
        .cOption("AutosaveDirectory") = sPDFPath
        .cOption("AutosaveFilename") = sPDFName
        .cOption("AutosaveFormat") = 0    ' 0 = PDF
        .cClearCache
    End With
    
    'demande plage de cellules
    Area = Application.InputBox(Prompt:="Plage de cellules (Exemple : D1:K8)")
    Set AreaA = ActiveSheet.Range(Area)
    
    With ActiveSheet.PageSetup 'tout sur une feuille
        .PrintArea = AreaA
        .Zoom = False
        .FitToPagesWide = 1
        .FitToPagesTall = 1
    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


Quand je l'essaye sur une sélection de cellules d'une feuille de classeur, ça marche. Quand je l'essaie sur un autre classeur, ça ne marche plus !
(Je mets toujours le code dans un module.)

Si vous avez une idée ou une solution à me proposer, merci d'avance !
 

Discussions similaires

Statistiques des forums

Discussions
312 488
Messages
2 088 835
Membres
103 972
dernier inscrit
steeter