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 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155
| Option Explicit
' ----------------------------------------------------------------
' Extraction des données à partir de fichier Word vers Excel
' Par : Grand Chaman Excel -- 2013/03/05
'-----------------------------------------------------------------
Sub Importation_Donnees_Word()
' -- Déclaration des variables
Dim wb As Workbook 'classeur Excel dans lequel on importe les données
Dim ws As Worksheet 'onglet Excel dans lequel on importe les données
Dim sChemin As String 'répertoire contenant les fichiers Word
Dim sNomFichier As String 'nom du fichier Word
Dim WApp As Object, WDoc As Object, WSel As Object
Dim i As Integer
' -- Initialisation des variables
Set wb = ThisWorkbook
Set ws = wb.Sheets(1) 'on sauvegarde dans la 1re feuille
sChemin = ChoisirRepertoire & "\" 'fonction pour choisir le répertoire contenant les fichier Word
'sChemin = ThisWorkbook.Path & "\" 'si les fichiers Word se trouvent dans le même répertoire que le fichier Excel
sNomFichier = Dir(sChemin & "*.doc*") 'pour ouvrir tous les fichiers .doc*. 1er fichier.
Set WApp = CreateObject("Word.Application") 'pour créer un objet Word
WApp.Visible = True 'ne pas afficher Word pendant l'exécution
i = ws.Range("A" & Rows.Count).End(xlUp).Row + 1 '1re ligne où on va écrire les données dans le fichier Excel
Application.ScreenUpdating = False
' -- Boucle sur les fichiers
Do While Len(sNomFichier) > 0
Set WDoc = WApp.Documents.Open(sChemin & sNomFichier) 'ouvre le document Word
Application.StatusBar = "Écriture ligne " & i 'message dans Excel pour voir la progression
' Nom du fichier
ws.Cells(i, 1) = sNomFichier
' Nom du produit (par la fonction FIND)
WApp.Selection.HomeKey unit:=6 'Retourne au début du fichier Word
WApp.Selection.Find.ClearFormatting 'on "vide la mémoire" de la fonction Recherche
WApp.Selection.Find.Execute "Nom du produit" 'On trouve le texte "Nom du produit"
WApp.Selection.MoveRight unit:=3, Count:=2, Extend:=2 'On se déplace de 3 mots
Set WSel = WApp.Selection 'sélection du texte trouvé
ws.Cells(i, 2) = Trim(Split(WSel, ":")(1)) 'Le Nom du produit est la 2e chaîne de caractères séparés par 2 ":"
' Nom du fournisseur (par la fonction FIND)
WApp.Selection.HomeKey unit:=6
WApp.Selection.Find.ClearFormatting
WApp.Selection.Find.Execute "Raison sociale"
WApp.Selection.MoveRight unit:=3, Count:=2, Extend:=1
Set WSel = WApp.Selection
ws.Cells(i, 3) = Split(WSel, ":")(1)
' Mentions de danger (par la fonction FIND)
WApp.Selection.HomeKey unit:=6
WApp.Selection.Find.ClearFormatting
WApp.Selection.Find.Execute "Mentions de danger et informations additionnelles sur les dangers :"
WApp.Selection.MoveRight unit:=3, Count:=2, Extend:=1
Set WSel = WApp.Selection
ws.Cells(i, 4) = Split(WSel, ":")(1)
' Numéro CE(par la fonction FIND)
' WApp.Selection.HomeKey unit:=6
' WApp.Selection.Find.ClearFormatting
' WApp.Selection.Find.Execute "EC :"
' WApp.Selection.MoveRight unit:=3, Count:=2, Extend:=1
' Set WSel = WApp.Selection
' ws.Cells(i, 5) = Split(WSel, ":")(1)
' Numéro REACH(par la fonction FIND)
' WApp.Selection.HomeKey unit:=6
' WApp.Selection.Find.ClearFormatting
' WApp.Selection.Find.Execute "REACH :"
' WApp.Selection.MoveRight unit:=3, Count:=2, Extend:=1
' Set WSel = WApp.Selection
' ws.Cells(i, 6) = Split(WSel, ":")(1)
' concentration (par la fonction FIND)
' WApp.Selection.HomeKey unit:=6
' WApp.Selection.Find.ClearFormatting
' WApp.Selection.Find.Execute "REACH :"
' WApp.Selection.MoveRight unit:=3, Count:=2, Extend:=1
' Set WSel = WApp.Selection
' ws.Cells(i, 7) = Split(WSel, ":")(1)
' Etat physique(par la fonction FIND)
WApp.Selection.HomeKey unit:=6
WApp.Selection.Find.ClearFormatting
WApp.Selection.Find.Execute "Etat physique :"
WApp.Selection.MoveRight unit:=3, Count:=2, Extend:=1
Set WSel = WApp.Selection
ws.Cells(i, 8) = Split(WSel, ":")(1)
' PH(par la fonction FIND)
WApp.Selection.HomeKey unit:=6
WApp.Selection.Find.ClearFormatting
WApp.Selection.Find.Execute "pH :"
WApp.Selection.MoveRight unit:=3, Count:=2, Extend:=1
Set WSel = WApp.Selection
ws.Cells(i, 9) = Split(WSel, ":")(1)
' Point d'éclair(par la fonction FIND)
WApp.Selection.HomeKey unit:=6
WApp.Selection.Find.ClearFormatting
WApp.Selection.Find.Execute "Point d'éclair :"
WApp.Selection.MoveRight unit:=3, Count:=2, Extend:=1
Set WSel = WApp.Selection
ws.Cells(i, 10) = Split(WSel, ":")(1)
' Point d'ébullition(par la fonction FIND)
WApp.Selection.HomeKey unit:=6
WApp.Selection.Find.ClearFormatting
WApp.Selection.Find.Execute "Point/intervalle d'ébullition :"
WApp.Selection.MoveRight unit:=3, Count:=2, Extend:=1
Set WSel = WApp.Selection
ws.Cells(i, 11) = Split(WSel, ":")(1)
' Pression de vapeur (par la fonction FIND)
WApp.Selection.HomeKey unit:=6
WApp.Selection.Find.ClearFormatting
WApp.Selection.Find.Execute "Pression de vapeur (50°C):"
WApp.Selection.MoveRight unit:=3, Count:=2, Extend:=1
Set WSel = WApp.Selection
ws.Cells(i, 12) = Split(WSel, ":")(1)
' Numéro ONU (par la fonction FIND)
' WApp.Selection.HomeKey unit:=6
' WApp.Selection.Find.ClearFormatting
' WApp.Selection.Find.Execute "Numéro ONU"
' WApp.Selection.MoveRight unit:=3, Count:=2, Extend:=1
' Set WSel = WApp.Selection
' ws.Cells(i, 13) = Split(WSel, ":")(1)
i = i + 1 'prochaine ligne
WDoc.Close False 'fermer le document Word sans enregistrer
sNomFichier = Dir 'prochain document
Loop
SortieNormale:
Application.ScreenUpdating = True
WApp.Quit 'Fermer l'instance de Word
Application.StatusBar = False 'Remise à zéro de la barre d'état
End Sub
Function ChoisirRepertoire() As String
' -- Fonction permettant de choisir un répertoire
Dim oRepertoire As Object
ChoisirRepertoire = ""
Set oRepertoire = CreateObject("Shell.Application").BrowseForFolder(0, "Choisir un répertoire", 0)
If (Not oRepertoire Is Nothing) Then ChoisirRepertoire = oRepertoire.Items.Item.Path
Set oRepertoire = Nothing
End Function |
Partager