Précédent   Forum des professionnels en informatique > Logiciels > Microsoft Office > Word > VBA Word
Partagez cette discussion sur d'autres réseaux sociaux : Viadeo Twitter Google Facebook Digg Delicious MySpace Yahoo
Réponse Proposer ce sujet en actualité
 
Outils de la discussion
Publicité
'
Vieux 19/04/2007, 09h55   #1
Membre du Club
 
Avatar de e040098k
 
Inscription : avril 2007
Messages : 197
Détails du profil
Informations forums :
Inscription : avril 2007
Messages : 197
Points : 56
Points : 56
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
e040098k est déconnecté   Envoyer un message privé Réponse avec citation 00
Vieux 19/04/2007, 10h48   #2
Membre éclairé
 
Inscription : octobre 2006
Messages : 390
Détails du profil
Informations forums :
Inscription : octobre 2006
Messages : 390
Points : 396
Points : 396
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.
dadavyvy est déconnecté   Envoyer un message privé Réponse avec citation 00
Vieux 19/04/2007, 11h14   #3
Membre du Club
 
Avatar de e040098k
 
Inscription : avril 2007
Messages : 197
Détails du profil
Informations forums :
Inscription : avril 2007
Messages : 197
Points : 56
Points : 56
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"
e040098k est déconnecté   Envoyer un message privé Réponse avec citation 00
Vieux 19/04/2007, 11h17   #4
Inactif
 
Avatar de ouskel'n'or
 
Inscription : février 2005
Messages : 12 466
Détails du profil
Informations forums :
Inscription : février 2005
Messages : 12 466
Points : 11 930
Points : 11 930
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)
ouskel'n'or est déconnecté   Envoyer un message privé Réponse avec citation 00
Vieux 19/04/2007, 11h28   #5
Membre du Club
 
Avatar de e040098k
 
Inscription : avril 2007
Messages : 197
Détails du profil
Informations forums :
Inscription : avril 2007
Messages : 197
Points : 56
Points : 56
Donc tous les "clsPDF_Creator" dans les déclarations sont à laissés?

Et il ne reconnait toujours pas ExportPDF !
e040098k est déconnecté   Envoyer un message privé Réponse avec citation 00
Vieux 19/04/2007, 11h58   #6
Membre éclairé
 
Inscription : octobre 2006
Messages : 390
Détails du profil
Informations forums :
Inscription : octobre 2006
Messages : 390
Points : 396
Points : 396
1/ il faut renommer le module de classe en "ExportPDF" et non pas "ExportPDF.cls"

2/ il faut activer la référence "PDFCreator"
dadavyvy est déconnecté   Envoyer un message privé Réponse avec citation 00
Vieux 19/04/2007, 12h19   #7
Inactif
 
Avatar de ouskel'n'or
 
Inscription : février 2005
Messages : 12 466
Détails du profil
Informations forums :
Inscription : février 2005
Messages : 12 466
Points : 11 930
Points : 11 930
Et pour ça il faut aller chercher PDFCreator.exe là où tu l'as mis. Dans l'éditeur VB -> Outils -> Références -> Parcourir
ouskel'n'or est déconnecté   Envoyer un message privé Réponse avec citation 00
Vieux 19/04/2007, 14h52   #8
Membre du Club
 
Avatar de e040098k
 
Inscription : avril 2007
Messages : 197
Détails du profil
Informations forums :
Inscription : avril 2007
Messages : 197
Points : 56
Points : 56
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 :
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 !!!
e040098k est déconnecté   Envoyer un message privé Réponse avec citation 00
Vieux 19/04/2007, 16h05   #9
Membre éclairé
 
Inscription : octobre 2006
Messages : 390
Détails du profil
Informations forums :
Inscription : octobre 2006
Messages : 390
Points : 396
Points : 396
Euh... Il ne fallait rien remplacer je crois... et ça aurait dû fonctionner...
dadavyvy est déconnecté   Envoyer un message privé Réponse avec citation 00
Vieux 19/04/2007, 16h20   #10
Membre du Club
 
Avatar de e040098k
 
Inscription : avril 2007
Messages : 197
Détails du profil
Informations forums :
Inscription : avril 2007
Messages : 197
Points : 56
Points : 56
Si je remplace rien il y a aussi une erreur de compilation !
e040098k est déconnecté   Envoyer un message privé Réponse avec citation 00
Vieux 19/04/2007, 16h24   #11
Membre éclairé
 
Inscription : octobre 2006
Messages : 390
Détails du profil
Informations forums :
Inscription : octobre 2006
Messages : 390
Points : 396
Points : 396
Et avec :
Code :
1
2
3
Private WithEvents PDFCreator1 As PDFCreator.clsPDFCreator
Private pErr As PDFCreator.clsPDFCreatorError
Private opt As PDFCreator.clsPDFCreatorOptions
dadavyvy est déconnecté   Envoyer un message privé Réponse avec citation 00
Vieux 19/04/2007, 16h27   #12
Membre du Club
 
Avatar de e040098k
 
Inscription : avril 2007
Messages : 197
Détails du profil
Informations forums :
Inscription : avril 2007
Messages : 197
Points : 56
Points : 56
IDEM Arg !!!

Ca m'énerve !
e040098k est déconnecté   Envoyer un message privé Réponse avec citation 00
Vieux 19/04/2007, 16h32   #13
Membre du Club
 
Avatar de e040098k
 
Inscription : avril 2007
Messages : 197
Détails du profil
Informations forums :
Inscription : avril 2007
Messages : 197
Points : 56
Points : 56
Pardon je retire c'a fonctionne !! Merci

Je continu et vous tiens au courant
e040098k est déconnecté   Envoyer un message privé Réponse avec citation 00
Vieux 19/04/2007, 17h17   #14
Membre du Club
 
Avatar de e040098k
 
Inscription : avril 2007
Messages : 197
Détails du profil
Informations forums :
Inscription : avril 2007
Messages : 197
Points : 56
Points : 56
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 :
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 :
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
e040098k est déconnecté   Envoyer un message privé Réponse avec citation 00
Réponse Proposer ce sujet en actualité Cette discussion est résolue.
Outils de la discussion



Fuseau horaire GMT +2. Il est actuellement 01h33.


 
 
 
 
Partenaires

Hébergement Web