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

VBA Word Discussion :

Export word vers PDF


Sujet :

VBA Word

  1. #1
    Membre régulier Avatar de e040098k
    Inscrit en
    Avril 2007
    Messages
    197
    Détails du profil
    Informations forums :
    Inscription : Avril 2007
    Messages : 197
    Points : 94
    Points
    94
    Par défaut Export word vers PDF
    Bonjour,

    J'ai trouvé une source très intéressante sur le site permettant de convertir un document word en pdf :
    http://www.developpez.net/forums/sho...d.php?t=211931
    C'est codé en vb, j'aimerais savoir si il est possible de faire la même chose en vba ?

    Merci

  2. #2
    Membre confirmé
    Profil pro
    Inscrit en
    Octobre 2006
    Messages
    393
    Détails du profil
    Informations personnelles :
    Localisation : France

    Informations forums :
    Inscription : Octobre 2006
    Messages : 393
    Points : 451
    Points
    451
    Par défaut
    Je vois que tu avais déjà posé la question sur l'autre sujet.

    Mais comme te le dit AlainTech, as-tu essayé?

    Vois-tu des différences avec du VBA? Si ce n'est qu'un command dans VB est un commandbutton dans VBA...
    De toute façon, il te faudra activer la référence PDFCreator, si tu l'as installé sur ton ordi.

  3. #3
    Membre régulier Avatar de e040098k
    Inscrit en
    Avril 2007
    Messages
    197
    Détails du profil
    Informations forums :
    Inscription : Avril 2007
    Messages : 197
    Points : 94
    Points
    94
    Par défaut
    Lorsque je la teste sous word

    il ne reconnait pas le type ExportPDF
    Alors que j'ai activer la référence PDFCreator

    Et je peux pas nommer le module de classe avec ".cls"

  4. #4
    Inactif  
    Avatar de ouskel'n'or
    Profil pro
    Inscrit en
    Février 2005
    Messages
    12 464
    Détails du profil
    Informations personnelles :
    Localisation : France

    Informations forums :
    Inscription : Février 2005
    Messages : 12 464
    Points : 15 543
    Points
    15 543
    Par défaut
    Un module de classe a l'extension cls par défaut lors de l'export, tu n'as pas à la mettre dans le nom
    De même, les modules ont l'extension bas, les userforms frm et les feuilles cls comme les modules de classe (mais le type, lui, change)

  5. #5
    Membre régulier Avatar de e040098k
    Inscrit en
    Avril 2007
    Messages
    197
    Détails du profil
    Informations forums :
    Inscription : Avril 2007
    Messages : 197
    Points : 94
    Points
    94
    Par défaut
    Donc tous les "clsPDF_Creator" dans les déclarations sont à laissés?

    Et il ne reconnait toujours pas ExportPDF !

  6. #6
    Membre confirmé
    Profil pro
    Inscrit en
    Octobre 2006
    Messages
    393
    Détails du profil
    Informations personnelles :
    Localisation : France

    Informations forums :
    Inscription : Octobre 2006
    Messages : 393
    Points : 451
    Points
    451
    Par défaut
    1/ il faut renommer le module de classe en "ExportPDF" et non pas "ExportPDF.cls"

    2/ il faut activer la référence "PDFCreator"

  7. #7
    Inactif  
    Avatar de ouskel'n'or
    Profil pro
    Inscrit en
    Février 2005
    Messages
    12 464
    Détails du profil
    Informations personnelles :
    Localisation : France

    Informations forums :
    Inscription : Février 2005
    Messages : 12 464
    Points : 15 543
    Points
    15 543
    Par défaut
    Et pour ça il faut aller chercher PDFCreator.exe là où tu l'as mis. Dans l'éditeur VB -> Outils -> Références -> Parcourir

  8. #8
    Membre régulier Avatar de e040098k
    Inscrit en
    Avril 2007
    Messages
    197
    Détails du profil
    Informations forums :
    Inscription : Avril 2007
    Messages : 197
    Points : 94
    Points
    94
    Par défaut
    D'accord il reconnait a présent mon module

    j'ai remplacer tous les "PDFCreator.cls" par des "ExportPDF"

    Seulement j'ai un problème au niveau des variables :
    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
     
    Private WithEvents PDFCreator1 As ExportPDFPDFCreator '/ composant principal
    Private pErr As ExportPDFError '/ classe de gestion d'erreur
    Private opt As ExportPDFOptions '/ classe de paramétrage
    Il me met une erreur de compilation !!!

  9. #9
    Membre confirmé
    Profil pro
    Inscrit en
    Octobre 2006
    Messages
    393
    Détails du profil
    Informations personnelles :
    Localisation : France

    Informations forums :
    Inscription : Octobre 2006
    Messages : 393
    Points : 451
    Points
    451
    Par défaut
    Euh... Il ne fallait rien remplacer je crois... et ça aurait dû fonctionner...

  10. #10
    Membre régulier Avatar de e040098k
    Inscrit en
    Avril 2007
    Messages
    197
    Détails du profil
    Informations forums :
    Inscription : Avril 2007
    Messages : 197
    Points : 94
    Points
    94
    Par défaut
    Si je remplace rien il y a aussi une erreur de compilation !

  11. #11
    Membre confirmé
    Profil pro
    Inscrit en
    Octobre 2006
    Messages
    393
    Détails du profil
    Informations personnelles :
    Localisation : France

    Informations forums :
    Inscription : Octobre 2006
    Messages : 393
    Points : 451
    Points
    451
    Par défaut
    Et avec :
    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    Private WithEvents PDFCreator1 As PDFCreator.clsPDFCreator
    Private pErr As PDFCreator.clsPDFCreatorError
    Private opt As PDFCreator.clsPDFCreatorOptions

  12. #12
    Membre régulier Avatar de e040098k
    Inscrit en
    Avril 2007
    Messages
    197
    Détails du profil
    Informations forums :
    Inscription : Avril 2007
    Messages : 197
    Points : 94
    Points
    94
    Par défaut
    IDEM Arg !!!

    Ca m'énerve !

  13. #13
    Membre régulier Avatar de e040098k
    Inscrit en
    Avril 2007
    Messages
    197
    Détails du profil
    Informations forums :
    Inscription : Avril 2007
    Messages : 197
    Points : 94
    Points
    94
    Par défaut
    Pardon je retire c'a fonctionne !! Merci

    Je continu et vous tiens au courant

  14. #14
    Membre régulier Avatar de e040098k
    Inscrit en
    Avril 2007
    Messages
    197
    Détails du profil
    Informations forums :
    Inscription : Avril 2007
    Messages : 197
    Points : 94
    Points
    94
    Par défaut
    je met le code finaliser (je n'ai fait que peut de modification par rapport a vb)

    Si cela peut servir à d'autre

    A mettre dans un userform avec:
    un bouton Traitement
    un bouton quitter
    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
    Option Explicit
     
    Dim clExp As New ExportPDF
    Dim Dialog1 As Object
    Dim sFichier() As String ' liste des fichiers DOC à convertir
    Dim sPathRacine As String, sFiles() As String
     
    Private Sub quitter_Click()
        Unload Me
    End Sub
     
    Private Sub Traitement_Click()
        Dim DefaultPrinter As String
        Dim i As Integer
        Set clExp = New ExportPDF
        '
        clExp.NomDir = "C:\Documents and Settings\stagiaire1\Bureau\"
        clExp.NomFichier = "test"
        clExp.ChemDocComplet = "C:\Documents and Settings\stagiaire1\Bureau\test.doc"
        '
        clExp.ConversionPDF
     
        Set clExp = Nothing
     
    End Sub

    A mettre dans une classe nommée ExportPDF :

    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
    Private pErr As PDFCreator.clsPDFCreatorError
    Private opt As PDFCreator.clsPDFCreatorOptions
     
     
    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
    Voila il faudrait le completer en incluant une boite de dialogue permettant de selectionner un ou plusieurs fichiers. Je m'en occupe dès ce soir !

    A bientôt et Merci

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

Discussions similaires

  1. Export Word vers PDF : pilotage du composant ActiveX PDFCreator
    Par Phifi dans le forum Vos contributions VB6
    Réponses: 7
    Dernier message: 02/03/2012, 16h40
  2. Logiciel word vers PDF qui garde les hyperlien ?
    Par elitost dans le forum Word
    Réponses: 30
    Dernier message: 20/11/2007, 21h30
  3. Convertir word vers pdf
    Par benjisan dans le forum VBA Access
    Réponses: 3
    Dernier message: 17/07/2007, 17h06
  4. [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
  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