• Initiateur de la discussion Initiateur de la discussion Dolichotis
  • Date de début Date de début

Boostez vos compétences Excel avec notre communauté !

Rejoignez Excel Downloads, le rendez-vous des passionnés où l'entraide fait la force. Apprenez, échangez, progressez – et tout ça gratuitement ! 👉 Inscrivez-vous maintenant !

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 !!
 
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 !
 
- Navigue sans publicité
- Accède à Cléa, notre assistante IA experte Excel... et pas que...
- Profite de fonctionnalités exclusives
Ton soutien permet à Excel Downloads de rester 100% gratuit et de continuer à rassembler les passionnés d'Excel.
Je deviens Supporter XLD

Discussions similaires

Réponses
3
Affichages
672
Réponses
2
Affichages
511
Réponses
10
Affichages
547
Retour