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
| Option Explicit
Sub ImportDataWord()
'---
Const cCriteria = "Analyses"
'---
Dim oWD As New Word.Application
Dim oDoc As Word.Document
Dim oTableObject As Word.Table
Dim oWordRange As Word.Range
Dim sDocname As String
Dim wb As Workbook
Dim wsRep As Worksheet
Dim wsMPCA As Worksheet
Dim wsPrel As Worksheet
Dim wsData As Worksheet
Dim i As Long
'---
Set wb = ThisWorkbook
Set wsRep = wb.Sheets("Repérages")
Set wsMPCA = wb.Sheets("MPCA")
Set wsPrel = wb.Sheets("Prélevements")
Set wsData = wb.Sheets("Data")
wsData.Cells.Clear
'--- Recherche du document word à parcourir
With Application.FileDialog(msoFileDialogFilePicker)
.AllowMultiSelect = False
.Filters.Add "Documents", "*.Doc*", 1
.Show
If .SelectedItems.Count > 0 Then
sDocname = .SelectedItems(1)
End If
End With
If Len(sDocname) = 0 Then Exit Sub '--- aucun doc
'--- On ouvre le document WORD
Set oDoc = oWD.Documents.Open(sDocname, , True)
oWD.Visible = True
oDoc.Activate
'--- récupérer données
With wsRep
i = .Range("E" & Rows.Count).End(xlUp).Row + 1 '--- n° nouvelle ligne
'--- Nom du fichier
.Cells(i, 8) = sDocname
'--- Nature des travaux (dans le 2e tableau, ligne 8, colonne 2)
.Cells(i, 5) = txt(oDoc.Tables(2).Cell(8, 2).Range)
'--- No (par la fonction FIND)
oWD.Selection.HomeKey Unit:=6 'Retourne au début du fichier Word
oWD.Selection.Find.ClearFormatting 'on "vide la mémoire" de la fonction Recherche
oWD.Selection.Find.Execute "N°" 'On trouve le texte "No"
oWD.Selection.MoveRight Unit:=3, Count:=2, Extend:=1 'On se déplace de 10 mots
.Cells(i, 7) = oWD.Selection 'sélection du texte trouvé
'--- Nom du fichier schema
.Cells(i, 9) = Mid(sDocname, InStrRev(sDocname, "\") + 1) & "-Schemas"
'--- Date de visite
oWD.Selection.HomeKey Unit:=6 'Retourne au début du fichier Word
oWD.Selection.Find.ClearFormatting 'on "vide la mémoire" de la fonction Recherche
oWD.Selection.Find.Execute "^#^#/^#^#/^#^#^#^#" 'On trouve une date
.Cells(i, 12) = oWD.Selection.Text 'sélection du texte trouvé --- ? split
'--- Date du rapport de reperage(dans le 2e tableau, ligne 1, colonne 2)
.Cells(i, 14) = txt(oDoc.Tables(2).Cell(1, 2).Range)
End With
'--- Nom du fichier feuille MPCA
wsMPCA.Cells(i, 2) = wsRep.Cells(i, 8)
'--- boucler sur les tableaux du document
For Each oTableObject In oDoc.Tables
Set oWordRange = oTableObject.Range
'--- rechercher le mot clé
oWordRange.Find.Execute FindText:=cCriteria, Forward:=True
If oWordRange.Find.Found = True Then
'--- recopier la table dans la feuille du classeur
oTableObject.Range.WholeStory
oTableObject.Range.Copy
wsData.Select
wsData.Range("A1").Select
wsData.Paste
End If
Next
'--- faire le ménage
oDoc.Close False
oWD.Quit
Set oWD = Nothing
Set oDoc = Nothing
Set oWordRange = Nothing
Set wsRep = Nothing
Set wsMPCA = Nothing
Set wsPrel = Nothing
Set wsData = Nothing
Set wb = Nothing
End Sub
Private Function txt(s As String) As String
'--- retirer les caractères de formatage Word
s = Replace(s, Chr(13), "")
s = Replace(s, Chr(7), "")
End Function |
Partager