IdentifiantMot de passe
Loading...
Mot de passe oublié ?Je m'inscris ! (gratuit)
Navigation

Inscrivez-vous gratuitement
pour pouvoir participer, suivre les réponses en temps réel, voter pour les messages, poser vos propres questions et recevoir la newsletter

Macros et VBA Excel Discussion :

Impression de fichier en pdf avec macro VBA [XL-2010]


Sujet :

Macros et VBA Excel

  1. #1
    Membre habitué
    Profil pro
    Inscrit en
    Avril 2007
    Messages
    794
    Détails du profil
    Informations personnelles :
    Localisation : France

    Informations forums :
    Inscription : Avril 2007
    Messages : 794
    Points : 186
    Points
    186
    Par défaut Impression de fichier en pdf avec macro VBA
    Bonjour à tous

    J’utilise pour imprimer la page active au format PDF le code de kiki29 qui a fait l’objet d’une autre discussion.

    Voilà la question :
    Si on utilise la fonction Excel =>imprimer=> choix de l’imprimante PDF CREATOR=>imprimer=>enregistrer, si le fichier PDF existe le message demande si il faut remplacer le fichier PDF existant.

    Comment intégrer ce choix dans le code de kiki29

    J’ai cherché sur plusieurs forums je n’ai pas trouvé de solution.

    Merci d’avance pour votre réponse.

  2. #2
    Expert éminent sénior
    Homme Profil pro
    aucune
    Inscrit en
    Septembre 2011
    Messages
    8 207
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Localisation : France, Paris (Île de France)

    Informations professionnelles :
    Activité : aucune

    Informations forums :
    Inscription : Septembre 2011
    Messages : 8 207
    Points : 14 362
    Points
    14 362
    Par défaut
    Bonjour,

    Pour vérifier l'existence d'un fichier, utilise et adapte le code suivant :

    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    5
    6
    7
    8
    9
        Dim FSO As Object, Chemin As String, Fich As String
        Set FSO = CreateObject("Scripting.FileSystemObject")
        Chemin = "c:\temp"
        Fich = "test.pdf"
        If FSO.FileExists(Chemin & "\" & Fich) = True Then
            MsgBox "le fichier existe"
        Else
            MsgBox "le fichier n'existe pas"
        End If
    oubien :

    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    5
        Dim Chemin As String, Fich As String
        Chemin = "c:\temp"
        Fich = "test.pdf"
        rep = Dir(Chemin & "\" & Fich)
        If rep = "" Then MsgBox "le fichier n'existe pas"

  3. #3
    Membre confirmé
    Profil pro
    Inscrit en
    Novembre 2007
    Messages
    473
    Détails du profil
    Informations personnelles :
    Localisation : France, Vendée (Pays de la Loire)

    Informations forums :
    Inscription : Novembre 2007
    Messages : 473
    Points : 493
    Points
    493
    Par défaut
    Salut à vous,

    Pour ma part je ne créer plus de PDF avec pdfCreator, car selon les version installé ça ne fonctionne pas tjrs bien. Je les créer directement avec excel mais pour les imprimer directement je ne sais pas ou alors la on donne par défaut l'imprimante PDFcreator:
    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    Sheets("Feuil1").Select
      Application.ActivePrinter = "PDFCreator sur Ne00:"
        ActiveWindow.SelectedSheets.PrintOut Copies:=1, ActivePrinter:= _
            "PDFCreator sur Ne00:", Collate:=True
    Sinon pour éviter le message d'erreur:
    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    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
    Dim pdfjob
    Dim NomExcel, NomPdf, DefaultPrinter
     Set pdfjob = CreateObject("PDFCreator.clsPDFCreator")
    NomExcel = ThisWorkbook.Name
    NomPdf = "Fxxxxx"  & ".pdf"
    With pdfjob
    If .cStart("/NoProcessingAtStartup") = False Then
    MsgBox "Impossible d'initialiser PDFCreator.", vbCritical + vbOKOnly, "PrtPDFCreator"
    GoTo eRReur
    Exit Sub
    End If
    .cOption("UseAutosave") = 1
    .cOption("UseAutisaveDirectory") = 1
    .cOption("AutosaveDirectory") = ThisWorkbook.Path ' chemin enregistrement du classeur actuel
    .cOption("AutosaveFilename") = NomPdf
    .cOption("AutosaveFormat") = 0
    .cClearCache
    End With
     
    'ThisWorkbook.PrintOut Copies:=1, ActivePrinter:="PDFCreator" 'imprime le classeur entier
    ThisWorkbook.Sheets("FeuilFact").PrintOut Copies:=1, ActivePrinter:="PDFCreator" 'imprime l'onglet pour mise en page définir la partie à imprimer
    'Selection.PrintOut Copies:=1, ActivePrinter:="PDFCreator" 'imprime la selection
     
    Do Until pdfjob.cCountOfPrintjobs = 1
    DoEvents
    Loop
    pdfjob.cPrinterStop = False
    Do Until pdfjob.cCountOfPrintjobs = 0
    DoEvents
    Loop
    With pdfjob
    .cDefaultPrinter = DefaultPrinter
    .cClearCache
    .cClose
    End With
    Voila, ma participation!

  4. #4
    Membre habitué
    Profil pro
    Inscrit en
    Avril 2007
    Messages
    794
    Détails du profil
    Informations personnelles :
    Localisation : France

    Informations forums :
    Inscription : Avril 2007
    Messages : 794
    Points : 186
    Points
    186
    Par défaut
    Bonjour jijie, Daniel.C

    J'ai combiné vos code :
    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    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
    Dim pdfjob
    Dim NomExcel, NomPdf, DefaultPrinter
    Set pdfjob = CreateObject("PDFCreator.clsPDFCreator")
    Dim Chemin As String, Fich As String, rep
    Chemin = "c:\temp"
    Fich = CreateObject("Scripting.FileSystemObject").GetBaseName(ActiveWorkbook.Name) & ".pdf"
    rep = Dir(Chemin & "\" & Fich)
     
    If rep = "" Then
        MsgBox "le fichier n'existe pas création du fichier PDFCreator"
    Impression:
        With pdfjob
            If .cStart("/NoProcessingAtStartup") = False Then
                MsgBox "Impossible d'initialiser PDFCreator.", vbCritical + vbOKOnly, "PrtPDFCreator"
                'GoTo Error
                Exit Sub
            End If
                .cOption("UseAutosave") = 1
                .cOption("UseAutisaveDirectory") = 1
                .cOption("AutosaveDirectory") = Chemin ' chemin enregistrement du classeur actuel
                .cOption("AutosaveFilename") = Fich
                .cOption("AutosaveFormat") = 0
                .cClearCache
        End With
     
        'ThisWorkbook.PrintOut Copies:=1, ActivePrinter:="PDFCreator" 'imprime le classeur entier
        ThisWorkbook.Sheets("Feuil1").PrintOut Copies:=1, ActivePrinter:="PDFCreator" 'imprime l'onglet pour mise en page définir la partie à imprimer
        'Selection.PrintOut Copies:=1, ActivePrinter:="PDFCreator" 'imprime la selection
     
        Do Until pdfjob.cCountOfPrintjobs = 1
            DoEvents
        Loop
     
        pdfjob.cPrinterStop = False
     
        Do Until pdfjob.cCountOfPrintjobs = 0
            DoEvents
        Loop
        With pdfjob
            .cDefaultPrinter = DefaultPrinter
            .cClearCache
            .cClose
        End With
     
    Else
        réponse = MsgBox("le fichier existe voulez-vous le remplacer ?", vbYesNo)
        If réponse = vbYes Then
            MsgBox "Remplacement du fichier existant"
            GoTo Impression
        Else
            MsgBox "Sortie de la procédure"
        End If
    End If
    Çà fonctionne nickel chrome, merci à vous.

    Faite moi part de vos commentaires svp.

  5. #5
    Invité
    Invité(e)
    Par défaut
    Bonjour

    Pourquoi utiliser PDF CREATOR ?

    Excel 2010 gère les PDF.

    Philippe

  6. #6
    Membre habitué
    Profil pro
    Inscrit en
    Avril 2007
    Messages
    794
    Détails du profil
    Informations personnelles :
    Localisation : France

    Informations forums :
    Inscription : Avril 2007
    Messages : 794
    Points : 186
    Points
    186
    Par défaut
    Bonjour Philippe JOCHMANS

    Effectivement c'est plus simple de gérer les PDF par Excel 2010.

    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    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
    Dim Chemin As String, Fich As String, Rep As String, CheminComplet As String
     
    Chemin = "c:\temp"
    Fich = CreateObject("Scripting.FileSystemObject").GetBaseName(ThisWorkbook.Name)
    CheminComplet = Chemin & "\" & Fich & ".pdf"
    Rep = Dir(Chemin & "\" & Fich & ".pdf")
     
    If Rep = "" Then
        réponse = MsgBox("Le fichier n'existe pas, création du fichier PDFCreator", vbYesNo)
        If réponse = vbYes Then
    Impression:
            ChDir Chemin
            ActiveSheet.ExportAsFixedFormat Type:=xlTypePDF, Filename:=Rep, _
                Quality:=xlQualityStandard, IncludeDocProperties:=True, IgnorePrintAreas _
                :=False, OpenAfterPublish:=False 'n'affiche pas le fichier PDF
        Else
            MsgBox "Sortie de la procédure"
            Exit Sub
        End If
    Else
        Réponse1 = MsgBox("le fichier existe voulez-vous le remplacer ?", vbYesNo)
        If Réponse1 = vbYes Then
            MsgBox "Remplacement du fichier existant"
            GoTo Impression
        Else
            MsgBox "Sortie de la procédure"
        End If
    End If
    Merci à vous pour votre aide.

  7. #7
    Inactif  

    Homme Profil pro
    cuisiniste
    Inscrit en
    Avril 2009
    Messages
    15 374
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Localisation : France, Var (Provence Alpes Côte d'Azur)

    Informations professionnelles :
    Activité : cuisiniste
    Secteur : Bâtiment

    Informations forums :
    Inscription : Avril 2009
    Messages : 15 374
    Points : 12 068
    Points
    12 068
    Billets dans le blog
    8
    Par défaut heu
    bonjour
    je rejoins l'opinion de philippe jochmans depuis 2007 l'impression en pdf fait parti des mises a jour disponibles pour office

    au plaisir

  8. #8
    Candidat au Club
    Homme Profil pro
    Inscrit en
    Juin 2011
    Messages
    2
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Localisation : Belgique

    Informations forums :
    Inscription : Juin 2011
    Messages : 2
    Points : 3
    Points
    3
    Par défaut
    Bon, ça fait plus de 2 ans que la conversation est clôturée mais je tenais à dire merci car elle vient de m'aider pour la création du PDF

+ Répondre à la discussion
Cette discussion est résolue.

Discussions similaires

  1. Recherche mot dans .PDF avec macro VBA
    Par klemsy20 dans le forum Macros et VBA Excel
    Réponses: 16
    Dernier message: 25/09/2019, 14h15
  2. fusion de fichiers texte avec macro VBa
    Par xak28 dans le forum Macros et VBA Excel
    Réponses: 4
    Dernier message: 30/11/2009, 07h28
  3. Ouvrir un fichier pdf avec Access VBA
    Par dflamme dans le forum VBA Access
    Réponses: 29
    Dernier message: 14/10/2008, 11h41
  4. Ouverture fichier openoffice avec macro VBA
    Par Christian Lo dans le forum Macros et VBA Excel
    Réponses: 1
    Dernier message: 26/08/2008, 05h48
  5. probleme de selection aleatoire sur excel avec macro vba
    Par guillaume sors dans le forum Macros et VBA Excel
    Réponses: 1
    Dernier message: 21/10/2005, 10h51

Partager

Partager
  • Envoyer la discussion sur Viadeo
  • Envoyer la discussion sur Twitter
  • Envoyer la discussion sur Google
  • Envoyer la discussion sur Facebook
  • Envoyer la discussion sur Digg
  • Envoyer la discussion sur Delicious
  • Envoyer la discussion sur MySpace
  • Envoyer la discussion sur Yahoo