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

Vos contributions VB6 Discussion :

Export Word vers PDF : pilotage du composant ActiveX PDFCreator [Sources]


Sujet :

Vos contributions VB6

  1. #1
    Membre éclairé
    Profil pro
    Inscrit en
    Mai 2006
    Messages
    730
    Détails du profil
    Informations personnelles :
    Localisation : France

    Informations forums :
    Inscription : Mai 2006
    Messages : 730
    Points : 876
    Points
    876
    Par défaut Export Word vers PDF : pilotage du composant ActiveX PDFCreator
    Sujet récurrent sur le forum, comment exporter un document Word en utilisant un composant gratuit.

    C'est une adaptation des sources fournies par PDFCreator ainsi que du fruit de mes recherches sur le net.

    A copier dans un module de classe "ExportPDF.cls"

    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
    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
    83
    84
    85
    86
    87
    88
    89
    90
    91
    92
    93
    94
    95
    96
    97
    98
    99
    100
    101
    102
    103
    104
    105
    106
    107
    108
    109
    110
    111
    112
    113
    114
    115
    116
    117
    118
    119
    120
    121
    122
    123
    124
    125
    126
    127
    128
    129
    130
    131
    132
    133
    134
    135
    136
    137
    138
    139
    140
    Option Explicit
    '''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
    '============================================================================================='
    ' ajouter la référence PDFCreator.exe au projet
    '============================================================================================='
    '''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
    Private mvarNomFichier As String
    Private mvarNomDir As String
    Private mvarChemDocComplet As String
    '
    ' IMPORTANT : le composant est déclaré avec ses évènements
    Private WithEvents PDFCreator1 As PDFCreator.clsPDFCreator '/ composant principal
    Private pErr As clsPDFCreatorError '/ classe de gestion d'erreur
    Private opt As clsPDFCreatorOptions '/ classe de paramétrage
     
    Private noStart As Boolean '/ variable de contrôle
     
    Private ImprimanteParDefaut As String '/variable imprimante par défaut
     
    Public Property Let ChemDocComplet(ByVal vData As String)
        '/ Propriété : chemin absolu du fichier à exporter (ex : C:\Temp\monword.doc)
        mvarChemDocComplet = vData
    End Property
     
    Public Property Get ChemDocComplet() As String
        ChemDocComplet = mvarChemDocComplet
    End Property
     
    Public Property Let NomDir(ByVal vData As String)
        '/ Propriété : chemin absolu du répertoire de sortie (ex : C:\Temp\)
        mvarNomDir = vData
    End Property
     
    Public Property Get NomDir() As String
        NomDir = mvarNomDir
    End Property
     
    Public Property Let NomFichier(ByVal vData As String)
        '/ Propriété : nom du fichier de sortie sans extension (ex : monarchive)
        mvarNomFichier = vData
    End Property
     
    Public Property Get NomFichier() As String
        NomFichier = mvarNomFichier
    End Property
     
    Private Sub Class_Initialize()
        '
        ' Instanciation des objets
        Set PDFCreator1 = New clsPDFCreator
        Set pErr = New clsPDFCreatorError
        '
        noStart = True
        '
        With PDFCreator1
            .cVisible = True
            If .cStart("/NoProcessingAtStartup") = False Then
               If .cStart("/NoProcessingAtStartup", True) = False Then
                   Exit Sub
               End If
               ' L'imprimante est occupée
               .cVisible = True
            End If
            '
            ' Instanciation de l'objet clsPDFCreatorOptions avec les options par défaut
            Set opt = .cOptions
            .cClearCache
            '/ A l'installation, PDFCreator mémorise l'imprimante système par défaut,
            '/ on la mémorise dans une variable
            ImprimanteParDefaut = .cDefaultPrinter
            ' on indique que l'imprimante a démarré
            noStart = False
        End With
        '
    End Sub
     
    Public Sub ConversionPDF()
        '
        ' On affecte les options de sortie qui nous intéressent à l'objet clsPDFCreatorOptions
        With opt
            '/ Répertoire de sortie
            .AutosaveDirectory = Trim$(NomDir)
            '/ Fichier de sortie
            .AutosaveFilename = Trim$(NomFichier)
            .UseAutosave = 1
            .UseAutosaveDirectory = 1
            '/ format de sortie (0 = PDF)
            .AutosaveFormat = 0
        End With
        '
        '/ on affecte ensuite les nouvelles options au composant principal clsPDFCreator
        Set PDFCreator1.cOptions = opt
        '/ On définit l'imprimante virtuelle comme imprimante par défaut
        PDFCreator1.cDefaultPrinter = "PDFCreator"
        '/ Impression du document Word (ou autre)
        PDFCreator1.cPrintFile Trim$(ChemDocComplet)
        '/ On affecte la propriété cPrinterStop à False pour contrôle ultérieur
        PDFCreator1.cPrinterStop = False
        '
        ' Tant que la propriété de cPrinterStop est à False, on laisse le temps au composant
        ' de terminer l'export PDF
        While PDFCreator1.cPrinterStop = False
            DoEvents
        Wend
        '
    End Sub
     
    Private Sub PDFCreator1_eReady() ' === ÉVÈNEMENT ==='
     
        '!!!! IMPORTANT!!!! c'est en interceptant l'évènement _eReady qu'on sait que l'impression PDF
        ' est terminé et l'imprimante libérée : cela permet de sortir de la boucle ci-dessus
        PDFCreator1.cPrinterStop = True
        '
    End Sub
     
    Private Sub PDFCreator1_eError() ' === ÉVÈNEMENT ==='
        Set pErr = PDFCreator1.cError
        MsgBox "Error[" & pErr.Number & "]: " & pErr.Description
        ' en cas d'erreur, on restaure l'imprimante par défaut du système via la classe principale
        PDFCreator1.cDefaultPrinter = ImprimanteParDefaut
        '
    End Sub
     
    Private Sub Class_Terminate()
        '
        '/ restauration de l'imprimante par défaut
        PDFCreator1.cDefaultPrinter = ImprimanteParDefaut
        '
        If noStart = False Then
            DoEvents
            PDFCreator1.cClose
        End If
        '
        DoEvents
        '
        Set PDFCreator1 = Nothing
        Set pErr = Nothing
        Set opt = Nothing
        '
    End Sub
    A copier dans un form :

    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
    Option Explicit
     
    Dim clExp As ExportPDF
     
    Private Sub Command1_Click()
        '
        Set clExp = New ExportPDF
        '
        clExp.NomDir = "C:\temp\"
        clExp.NomFichier = "test"
        clExp.ChemDocComplet = "C:\temp\test.doc"
        '
        clExp.ConversionPDF
        '
        Set clExp = Nothing
        '
    End Sub
    C'est volontairement simpliste dans la gestion des noms de fichiers, j'ai pris le parti d'aller à l'essentiel pour une bonne compréhension.

  2. #2
    Expert éminent sénior


    Profil pro
    Inscrit en
    Juin 2003
    Messages
    14 008
    Détails du profil
    Informations personnelles :
    Localisation : France

    Informations forums :
    Inscription : Juin 2003
    Messages : 14 008
    Points : 20 038
    Points
    20 038
    Par défaut
    Citation Envoyé par Phifi
    Ben merci à toi
    ça fait bientôt un an que j'ai posté ce source, ça fait plaisir d'avoir enfin un retour positif, car vu le nombre de lecture, j'imagine qu'il a dû servir à d'autres...
    Oui on à hésité ... à le mettre dans la dernière mise à jour des sources... mais j'ai pas eu (pris) le temps de le tester .... de plus j'aurai bien aimé un "zip" prêt à l'emploi....
    où mieux ... un petit tuto... reprenant par exemple l'installation de PdfCreator, un exemple de code commenté.... et en pièce jointe un projet complet... si tu te le "sent" Phifi... fais nous signe...

  3. #3
    Membre éclairé
    Profil pro
    Inscrit en
    Mai 2006
    Messages
    730
    Détails du profil
    Informations personnelles :
    Localisation : France

    Informations forums :
    Inscription : Mai 2006
    Messages : 730
    Points : 876
    Points
    876
    Par défaut
    Hé bien pourquoi, cela dit, j'aurais bien aimé approfondir quelques points comme la gestion de la file d'attente plutôt que l'impression à l'unité (que l'on peut boucler sans pb cependant)

    Vais essayer de préparer un truc avant 2012, promis !

  4. #4
    Expert éminent sénior


    Profil pro
    Inscrit en
    Juin 2003
    Messages
    14 008
    Détails du profil
    Informations personnelles :
    Localisation : France

    Informations forums :
    Inscription : Juin 2003
    Messages : 14 008
    Points : 20 038
    Points
    20 038
    Par défaut
    Citation Envoyé par Phifi
    Hé bien pourquoi, cela dit, j'aurais bien aimé approfondir quelques points comme la gestion de la file d'attente plutôt que l'impression à l'unité (que l'on peut boucler sans pb cependant)

    Vais essayer de préparer un truc avant 2012, promis !
    en plus il as du succès ton code ... j'ai un peu séparé les discussions histoire d'y voir plus clair :

    kevinou55 [PdfCreator] probléme pour détection fin d'impresion

    CoolyBonzy pdfcreator pdf vers Tiff

    Thierry_94 PdfCreator problémes de blocages OLE

    cashern Probléme avec PDFcreator

  5. #5
    Futur Membre du Club
    Inscrit en
    Février 2005
    Messages
    6
    Détails du profil
    Informations forums :
    Inscription : Février 2005
    Messages : 6
    Points : 7
    Points
    7
    Par défaut Problème sous Vista
    J'utilise cette classe avec bonheur dabs VB6 sous Windows 2000. Le parc passe sous Vista et là, cela ne fonctionne plus. Le code s'arrete tout simplement, pas de messages d'erreur, le programme ne plante pas mais tout ce qui est derrière la conversion ne s'exécute pas.
    Le code s'arrete dans Class_Initialize à la ligne : Set PDFCreator1 = New clsPDFCreator
    Là, Word démarre, charge le fichier, imprime et quitte, puis plus rien. Je peux fermer mon formulaire et qyuitter comme si rien ne s'était passé !!
    Je n'y comprend plus rien.
    Si une âme charitable pouvait m'aider..............
    D'avance, merci.

  6. #6
    Futur Membre du Club
    Inscrit en
    Mars 2009
    Messages
    8
    Détails du profil
    Informations forums :
    Inscription : Mars 2009
    Messages : 8
    Points : 8
    Points
    8
    Par défaut Probleme OLE Action
    Bonjour,

    Tout d'abord merci pour ce petit "tuto" très efficace.

    J'ai tout de même un léger problème : lorsque j'exécute ma méthode DFCreator1.cPrintFile, une fenêtre Adobe Reader s'ouvre et je dois la fermer manuellement pour que le code continue de s'exécuter. Si je ne la ferme pas à la main, j'ai forcément au bout d'un moment un message "Wating OLE Action etc..."

    Avez-vous déjà rencontré le problème ? Et si oui, y'a-t-il un moyen de le contourner ?

    D'avance merci beaucoup à tous !

  7. #7
    Membre régulier
    Homme Profil pro
    Consultant
    Inscrit en
    Juin 2007
    Messages
    77
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Localisation : France, Paris (Île de France)

    Informations professionnelles :
    Activité : Consultant
    Secteur : Conseil

    Informations forums :
    Inscription : Juin 2007
    Messages : 77
    Points : 93
    Points
    93
    Par défaut parfait
    très utile

  8. #8
    Candidat au Club
    Profil pro
    Inscrit en
    Mars 2012
    Messages
    3
    Détails du profil
    Informations personnelles :
    Localisation : France

    Informations forums :
    Inscription : Mars 2012
    Messages : 3
    Points : 2
    Points
    2
    Par défaut impression
    Bonjour,

    En testant sur VB ça marche avec la version d'essai Adobe Pro.Je cherche les différents paramétres que l'on customiser sur l'imprimante (recto/verso, A4, A5, ...)
    Est-ce que quelqu'un a un lien ?

    Tasaman

Discussions similaires

  1. Logiciel word vers PDF qui garde les hyperlien ?
    Par elitost dans le forum Word
    Réponses: 30
    Dernier message: 20/11/2007, 21h30
  2. Convertir word vers pdf
    Par benjisan dans le forum VBA Access
    Réponses: 3
    Dernier message: 17/07/2007, 17h06
  3. [FPDF] Convertir fichier Word vers PDF et vis versa
    Par young077 dans le forum Bibliothèques et frameworks
    Réponses: 1
    Dernier message: 06/07/2007, 10h59
  4. Export word vers PDF
    Par e040098k dans le forum VBA Word
    Réponses: 13
    Dernier message: 19/04/2007, 17h17
  5. [COM] Conversion WORD vers PDF sous PHP sans COM ?
    Par pdtor dans le forum Bibliothèques et frameworks
    Réponses: 2
    Dernier message: 31/01/2007, 22h27

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