Précédent   Forum des professionnels en informatique > Logiciels > Microsoft Office > Excel > Macros et VBA Excel
Macros et VBA Excel Vos questions relatives aux macros Excel, à l'utilisation de VBA et à l'automatisation de vos classeurs Excel.
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 28/09/2011, 23h29   #1
Invité de passage
 
Inscription : août 2009
Messages : 24
Détails du profil
Informations personnelles :
Âge : 41

Informations forums :
Inscription : août 2009
Messages : 24
Points : 4
Points : 4
Par défaut lecture de fichier texte

bonjour a tous
je recupere grace a pdf2txt un tableau pdf dans un fichier txt
mon probleme est que lorsque je l importe dans excel j obtiens 1 ligne complete par cellule quand j aimerais recuperer chaque valeur dans 1 cellule diffrente
cela est il possible en adaptant le code joint
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
Sub OpenPDF()
     '-------------------------------------------'
     ' You need to create a bat file first with one single line of text
     ' pdftotext.exe -layout YourPage.pdf
     ' DOWNLOAD LINK: http://www.foolabs.com/xpdf/download.html
     '-------------------------------------------'
 
     ' these lines look for a pdf file in your My Documents folder
    Set WshShell = CreateObject("WScript.Shell")
    ChDir (WshShell.SpecialFolders("MyDocuments"))
    PageName = Application.GetOpenFilename("YourPage, *.pdf", , "YourPage")
 
     ' if no file is picked the macro ends
    If PageName = "False" Then
        Exit Sub
    End If
 
     ' copies and renames the pdf file to the pdf2txt folder
    FileCopy PageName, "C:\pdf2txt\YourPage.pdf"
    ChDir ("C:\pdf2txt")
 
     ' THE BATFILE CONTAINS ONLY 1 LINE:
     ' pdftotext.exe -layout YourPage.pdf
 
    TestValue = Shell("YourPage.bat", 1)
 
     ' because the bat file runs for 1 or 2 seconds (in my case)
     ' I let the Excel macro wait 5 seconds before doing anything else
     ' there are more ingenious ways for VBA to wait for the end of an
     ' application, but this suits me fine...
 
    Application.Wait (Now + TimeValue("0:00:50"))
 
    ChDir "C:\pdf2txt"
    PageName = "C:\pdf2txt\YourPage.txt"
 
     ' the following reads the text that has been generated
    ReadTextFile (PageName)
 
     ' insert your text parsing - text to columns - ingenious vba stuff hereafter...
 
End Sub
 
Sub ReadTextFile(chemin As String)
    Dim FileNum As Integer
    Dim r As Variant
    Dim wb As Workbook
    Dim Data As String
    r = 1
    FileNum = FreeFile
    Set wb = Workbooks.Add
    Open chemin For Input As #FileNum
    Do While Not EOF(FileNum)
        Line Input #FileNum, Data
        ActiveSheet.Cells(r, 1) = Data
        r = r + 1
    Loop
    Close #FileNum
End Sub
je vous joins un fichier txt modele de l extraction

merci d avance
Fichiers attachés
Type de fichier : txt exemplae.txt (1,1 Ko, 10 affichages)
djdmd est déconnecté   Envoyer un message privé Réponse avec citation 00
Vieux 28/09/2011, 23h45   #2
Expert Confirmé
 
Homme Philippe
ex Observeur CGG / Analyste prog.
Inscription : juin 2006
Messages : 1 704
Détails du profil
Informations personnelles :
Nom : Homme Philippe
Localisation : France, Finistère (Bretagne)

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

Informations forums :
Inscription : juin 2006
Messages : 1 704
Points : 3 625
Points : 3 625
Salut, si cela peut t'aider voir sur http://www.developpez.net/forums/d43...bat-distiller/ posts # 2 et 3 pour la récup du texte d'un PDF ( surtout #3 à tenter avec vbTab )
__________________
Pensez à Voter, d'avance merci. ( Pouces en bas à la droite de Citer )
Balisez votre code après l'avoir indenté sous Excel via Smart Indenter
Autre utilitaire : MZ Tools 3.0 VBA

Contribution : Excel / Word / PDF avec Adobe Acrobat Pro / PDFCreator 1 2
kiki29 est déconnecté   Envoyer un message privé Réponse avec citation 00
Vieux 03/10/2011, 09h49   #3
Invité de passage
 
Inscription : août 2009
Messages : 24
Détails du profil
Informations personnelles :
Âge : 41

Informations forums :
Inscription : août 2009
Messages : 24
Points : 4
Points : 4
merci mais cela ne m aide pas vraiment car le texte est recupere format brut donc equivalent a ce que je fais actuellement
djdmd est déconnecté   Envoyer un message privé Réponse avec citation 00
Vieux 03/10/2011, 13h23   #4
Expert Confirmé
 
Homme Philippe
ex Observeur CGG / Analyste prog.
Inscription : juin 2006
Messages : 1 704
Détails du profil
Informations personnelles :
Nom : Homme Philippe
Localisation : France, Finistère (Bretagne)

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

Informations forums :
Inscription : juin 2006
Messages : 1 704
Points : 3 625
Points : 3 625
Salut, par curiosité serait-il possible d'avoir un échantillon de fichier PDF ?
__________________
Pensez à Voter, d'avance merci. ( Pouces en bas à la droite de Citer )
Balisez votre code après l'avoir indenté sous Excel via Smart Indenter
Autre utilitaire : MZ Tools 3.0 VBA

Contribution : Excel / Word / PDF avec Adobe Acrobat Pro / PDFCreator 1 2
kiki29 est déconnecté   Envoyer un message privé Réponse avec citation 00
Vieux 03/10/2011, 14h03   #5
Membre confirmé
 
Inscription : juillet 2007
Messages : 209
Détails du profil
Informations forums :
Inscription : juillet 2007
Messages : 209
Points : 219
Points : 219
Bonjour ,
un truc tout simple que j'ai récupéré un jour dans un autre post.
lorsque tu fais ton Line input , tu fais un split pour découper chaque champs , à toi de déterminer le Separateur dans ton cas.

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
 
 
 
Sub ReadTextFile(chemin As String)
    Dim FileNum As Integer
    Dim r As Variant
    Dim wb As Workbook
    Dim Data As String
    r = 1
    FileNum = FreeFile
    Set wb = Workbooks.Add
    Open chemin For Input As #FileNum
    Do While Not EOF(FileNum)
 
       Line Input #FileNum, Data
 
    tableau = Split(Data, Separateur)
  For i = 0 To UBound(tableau)
      ActiveSheet.Cells(r, i + 1) = tableau (i)
next i
        r = r + 1
    Loop
    Close #FileNum
End Sub
CodeFacile est déconnecté   Envoyer un message privé Réponse avec citation 00
Vieux 05/10/2011, 14h49   #6
Invité de passage
 
Inscription : août 2009
Messages : 24
Détails du profil
Informations personnelles :
Âge : 41

Informations forums :
Inscription : août 2009
Messages : 24
Points : 4
Points : 4
merci c est nettement mieux avec cette commande split
je fais des tests et je vous tiens au courant


apres divers test c est pas mal par contre j ai un nombre aleatoire d espace entre chaque colonne comment puis je dire cela a split pour qu il me les supprime au de la de 1
merci par avance de vos reponses
djdmd est déconnecté   Envoyer un message privé Réponse avec citation 00
Vieux 05/10/2011, 16h58   #7
Membre éclairé
 
Inscription : février 2006
Messages : 287
Détails du profil
Informations forums :
Inscription : février 2006
Messages : 287
Points : 348
Points : 348
Bonjour,
tu peux remplacer les double espaces par un simple espace, et boucler tant qu'il y en a. Ce n'est pas optimal mais bon, sauf si le fichier à traiter est énorme ça devrait à peine être plus lent.

Code :
1
2
3
4
5
6
7
8
9
10
    Do While Not EOF(FileNum)
 
       Line Input #FileNum, Data
 
       do while instr(Data, "  ") > 0 'on teste s'il y a des double espaces
           replace(Data, "  ", " ") 'on remplace les double espaces par un simple espace
       loop
 
    tableau = Split(Data, " ")
  For i = 0 To UBound(tableau)
neupont est déconnecté   Envoyer un message privé Réponse avec citation 00
Vieux 12/12/2011, 15h20   #8
Invité de passage
 
Inscription : août 2009
Messages : 24
Détails du profil
Informations personnelles :
Âge : 41

Informations forums :
Inscription : août 2009
Messages : 24
Points : 4
Points : 4
Par défaut merci de cette reponse

desole de ne pas avoir repondu plus tot
c est nickel ça marche bien
djdmd 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 20h43.


 
 
 
 
Partenaires

Hébergement Web