Problème de transformation Word to PDF via Excel
Mesdames, Messieurs: Bonjour!
Pour un projet assez important, je planche dessus depuis 4 mois, je dois aller dans un répertoire pour un récupérer les noms des fichiers présents. S'ils sont sous formats PDF, j'ai juste à tester leur existence dans un listing et les déplacer en fonction. S'ils sont sous format word, je me dois de les transformer en PDF, avant de faire la transformation.
Voici le sous programme:
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
| Menu = 0
Trans.Range("A1:A" & Trans.Range("A65536").End(xlUp).Row).Delete
Call listingfichier(AdresseTransforme, Menu)
NombreCV = Trans.Range("A65536").End(xlUp).Row
NombreDE = List.Range("A65536").End(xlUp).Row
For Recurrence = 1 To NombreCV
ExtensionCV = Right(Trans.Range("A" & Recurrence).Value, 3)
If ExtensionCV = "pdf" Then
For Revolution = 2 To NombreDE
Tempo = Left(Trans.Range("A" & Recurrence).Value, (InStr(Trans.Range("A" & Recurrence).Value, ".") - 1))
If List.Range("A" & Revolution).Value = Tempo Then
FichierSource = AdresseTransforme & Trans.Range("A" & Recurrence).Value
FichierDestination = AdresseCvValide & Trans.Range("A" & Recurrence).Value
FileCopy FichierSource, FichierDestination
Kill FichierSource
GoTo cvsuivant
End If
If Revolution = NombreDE Then
FichierSource = AdresseTransforme & Trans.Range("A" & Recurrence).Value
FichierDestination = AdresseCvOut & Trans.Range("A" & Recurrence).Value
FileCopy FichierSource, FichierDestination
Kill FichierSource
End If
Next Revolution
End If
If ExtensionCV = "doc" Then
Tempo = Left(Trans.Range("A" & Recurrence).Value, (InStr(Trans.Range("A" & Recurrence).Value, ".") - 1))
Tempo2 = Trans.Range("A" & Recurrence).Value
FichierSource = Tempo2
FichierDestination = Tempo & ".pdf"
For Revolution = 2 To NombreDE
If List.Range("A" & Revolution).Value = Tempo Then
Call WordtoPDF(AdresseTransforme, FichierDestination, FichierSource)
' Application.Wait (Now() + TimeValue("0:00:01"))
' Kill FichierSource
End If
Next Revolution
End If
cvsuivant:
Next Recurrence
MsgBox "Transformation effectuée!" |
Voici le module appelé (il est dans un module unique):
Code:
1 2 3 4 5 6 7 8 9
| Public Sub WordtoPDF(CVTransforme As String, fichier_dtn As String, fichier_src As String)
Dim clExp As ExportPDF
Set clExp = New ExportPDF
clExp.NomDir = CVTransforme
clExp.NomFichier = fichier_dtn
clExp.ChemDocComplet = CVTransforme & fichier_src
clExp.ConversionPDF
Set clExp = Nothing
End Sub |
Et le module de Classe:
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
| Option Explicit
' declaration des variables
Private mvarNomFichier As String, mvarNomDir As String, mvarCheminDocComplet As String
' IMPORTANT: le compsoant est declare avec ses evenements
Private WithEvents PDFCreator1 As PDFCreator.clsPDFCreator ' composant principal
Private pErr As clsPDFCreatorError ' classe de gestion d'erreur
Private opt As clsPDFCreatorOptions ' classe de parametrage
Private noStart As Boolean ' variable de controle
Private ImprimanteParDefaut As String ' variable imprimante par defaut
Public Property Let ChemDocComplet(ByVal vData As String)
' Propriete : chemin absolu du fichier a 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)
' Propriete : chemin absolu du repertoire 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)
' Propriete : 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 occupee
.cVisible = True
End If
'
' Instanciation de l objet clsPDFCreatorOptions avec les options par defaut
Set opt = .cOptions
.cClearCache
' A l installation, PDFCreator memorise l imprimante systeme par defaut,
' on la memorise dans une variable
ImprimanteParDefaut = .cDefaultPrinter
' on indique que l imprimante a demarre
noStart = False
End With
'
End Sub
Public Sub ConversionPDF()
' On affecte les options de sortie qui nous interessent a l objet clsPDFCreatorOptions
With opt
' Repertoire 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 definit l imprimante virtuelle comme imprimante par defaut
PDFCreator1.cDefaultPrinter = "PDFCreator"
' Impression du document Word (ou autre)
PDFCreator1.cPrintFile Trim$(ChemDocComplet)
' On affecte la propriete cPrinterStop a False pour controle ulterieur
PDFCreator1.cPrinterStop = False
' Tant que la propriete de cPrinterStop est a False, on laisse le temps au composant
' de terminer l export PDF
While PDFCreator1.cPrinterStop = False
DoEvents
Wend
End Sub
Private Sub PDFCreator1_eReady() ' === eVeNEMENT ==='
'!!!! IMPORTANT!!!! c est en interceptant l evenement _eReady qu on sait que l impression PDF
' est termine et l imprimante liberee : cela permet de sortir de la boucle ci-dessus
PDFCreator1.cPrinterStop = True
End Sub
Private Sub PDFCreator1_eError() ' === eVeNEMENT ==='
Set pErr = PDFCreator1.cError
MsgBox "Error[" & pErr.Number & "]: " & pErr.Description
' en cas d erreur, on restaure l imprimante par defaut du systeme via la classe principale
PDFCreator1.cDefaultPrinter = ImprimanteParDefaut
End Sub
Private Sub Class_Terminate()
' restauration de l imprimante par defaut
PDFCreator1.cDefaultPrinter = ImprimanteParDefaut
If noStart = False Then
DoEvents
PDFCreator1.cClose
End If
DoEvents
Set PDFCreator1 = Nothing
Set pErr = Nothing
Set opt = Nothing
End Sub |
J'ai coché les références suivantes:
- Microsoft Scripting Runtime
- Visual Basic for Applications
.
Lorsque je lance la marco, j'obtiens l'erreur:
Citation:
Erreur de compilation: Type défini par l'utilisateur non défini
avec les lignes suivantes en jaune:
Citation:
Public Sub WordtoPDF(CVTransforme As String, fichier_dtn As String, fichier_src As String)
Dim clExp As ExportPDF
.
Je tourne sur ce problème depuis 15 jours et je ne trouve pas donc si des yeux neufs peuvent m'orienter je ne dis pas non.