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 :

Problème de transformation Word to PDF via Excel [XL-2003]


Sujet :

Macros et VBA Excel

Vue hybride

Message précédent Message précédent   Message suivant Message suivant
  1. #1
    Membre averti
    Homme Profil pro
    Ressources humaines
    Inscrit en
    Août 2011
    Messages
    39
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Localisation : France, Haut Rhin (Alsace)

    Informations professionnelles :
    Activité : Ressources humaines
    Secteur : Service public

    Informations forums :
    Inscription : Août 2011
    Messages : 39
    Par défaut 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 : 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
    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 : Sélectionner tout - Visualiser dans une fenêtre à part
    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 : 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
    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 Script Control

    • Microsoft Scripting Runtime

    • Visual Basic for Applications

    • PDFCreator
    .
    Lorsque je lance la marco, j'obtiens l'erreur:
    Erreur de compilation: Type défini par l'utilisateur non défini
    avec les lignes suivantes en jaune:
    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.

  2. #2
    Expert confirmé
    Avatar de kiki29
    Homme Profil pro
    ex Observeur CGG / Analyste prog.
    Inscrit en
    Juin 2006
    Messages
    6 132
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Localisation : France, Finistère (Bretagne)

    Informations professionnelles :
    Activité : ex Observeur CGG / Analyste prog.

    Informations forums :
    Inscription : Juin 2006
    Messages : 6 132
    Par défaut
    Salut, une remarque maintes fois faite : à partir d'Office 2007 SP2 le format PDF est intégré en natif dans la suite Office, et donc ici tu n'as pas besoin de PDFCreator, Word permettant de sauver en PDF. Cela allègerait d'autant le code.

  3. #3
    Membre averti
    Homme Profil pro
    Ressources humaines
    Inscrit en
    Août 2011
    Messages
    39
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Localisation : France, Haut Rhin (Alsace)

    Informations professionnelles :
    Activité : Ressources humaines
    Secteur : Service public

    Informations forums :
    Inscription : Août 2011
    Messages : 39
    Par défaut
    Je sais kiki29. Mais le service IT de mon entreprise n'a pas fait la maj du Pack Office intégrant cette solution.
    Je suis donc obligé de passer par cette étape que j'avais travaillé avec Patricktoulon et Bbil. Cela fonctionnait parfaitement jusqu'à une mise à jour système et une modification d'un sous programme (qui n'appelle rien au niveau du PDF).

  4. #4
    Expert confirmé
    Avatar de kiki29
    Homme Profil pro
    ex Observeur CGG / Analyste prog.
    Inscrit en
    Juin 2006
    Messages
    6 132
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Localisation : France, Finistère (Bretagne)

    Informations professionnelles :
    Activité : ex Observeur CGG / Analyste prog.

    Informations forums :
    Inscription : Juin 2006
    Messages : 6 132
    Par défaut
    Re, alors pourquoi parler de 2010 dans le titre du Post si tu ne peux pas utiliser la possibilité offerte par cette version de sauver en Pdf ?

  5. #5
    Membre averti
    Homme Profil pro
    Ressources humaines
    Inscrit en
    Août 2011
    Messages
    39
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Localisation : France, Haut Rhin (Alsace)

    Informations professionnelles :
    Activité : Ressources humaines
    Secteur : Service public

    Informations forums :
    Inscription : Août 2011
    Messages : 39
    Par défaut
    Bonsoir,

    Je parle d'Excel 2010 car je programme et utilise la macro sous Excel 2003 au boulot. Comme je n'ai pas assez de temps alloué pour le faire, je développe aussi chez moi avec Office 2010.

  6. #6
    Expert confirmé
    Avatar de kiki29
    Homme Profil pro
    ex Observeur CGG / Analyste prog.
    Inscrit en
    Juin 2006
    Messages
    6 132
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Localisation : France, Finistère (Bretagne)

    Informations professionnelles :
    Activité : ex Observeur CGG / Analyste prog.

    Informations forums :
    Inscription : Juin 2006
    Messages : 6 132
    Par défaut
    Re, bravo, pour semer la confusion c'est parfait. Donc en fait c'est uniquement 2003. Je n'ai donc pas de solution.

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

Discussions similaires

  1. [XL-2010] Problème de sauvegarde Word et Pdf depuis macro excel
    Par Cllem_501 dans le forum Macros et VBA Excel
    Réponses: 7
    Dernier message: 14/11/2014, 15h03
  2. [XL-2007] Complement impression fichier pdf via excel
    Par capi81 dans le forum Macros et VBA Excel
    Réponses: 8
    Dernier message: 21/02/2012, 00h07
  3. Générer fichiers word ou pdf via java.
    Par tapoutapou dans le forum Documents
    Réponses: 4
    Dernier message: 15/07/2010, 18h02
  4. Problème avec Contrôle Activex Adobe Pdf Reader (Excel 2003)
    Par Godzestla dans le forum Macros et VBA Excel
    Réponses: 4
    Dernier message: 30/09/2008, 16h31
  5. transformer word en pdf
    Par thobass dans le forum VBA Word
    Réponses: 0
    Dernier message: 02/10/2007, 11h07

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