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
| Option Explicit
'--- adapté de
'--- https://www.excelguru.ca/forums/showthread.php?8900-Help-with-VBA-to-extract-data-from-Word-to-Excel
'--- https://stackoverflow.com/questions/40932675/excel-vba-import-word-table-with-merged-cells-to-excel
Sub GetTableData()
'===> réference Microsoft Word Object
Application.ScreenUpdating = False
Dim wdApp As New Word.Application
Dim wdDoc As Word.Document
Dim wdTbl As Word.Table
Dim wdCell As Word.Cell
Dim strFolder As String
Dim strFile As String
Dim WkBk As Workbook
Dim WkSht As Worksheet
Dim iRow As Long 'n° ligne dans tableau Word
Dim iCol As Integer 'n° colonne dans tableau Word
Dim tableNo As Integer 'n° tableau dans Word
Dim tableTot As Integer 'nb tableaux dans Word
Dim kRow As Long 'n° ligne dans Excel
Dim s As String
'strFolder = GetFolder
'If strFolder = "" Then GoTo ErrExit
strFolder = ThisWorkbook.Path '--- à adapter
Set WkBk = ActiveWorkbook
'--- annule le démarrage automatique des macros dans Word
wdApp.WordBasic.DisableAutoMacros
strFile = Dir(strFolder & "\*.docx", vbNormal) '--- à adapter
kRow = 1
While strFile <> ""
Debug.Print strFile;
Set wdDoc = wdApp.Documents.Open(Filename:=strFolder & "\" & strFile, ReadOnly:=True, AddToRecentFiles:=False, Visible:=False)
'--- ReadOnly:=True évite erreur si document ouvert ailleurs
With wdDoc
tableTot = wdDoc.Tables.Count
If tableTot = 0 Then
Debug.Print ": aucune table"
Else
Debug.Print ": " & tableTot & " tables"
For tableNo = 1 To tableTot
With .Tables(tableNo)
Cells(kRow, 1) = strFile & " - Tableau " & tableNo & "/" & tableTot
'--- copier les cellules du tableau Word dans la feuille Excel
'For iRow = 1 To .Rows.Count
'For iCol = 1 To .Columns.Count
'--- plus simple mais provoque une erreur s'il y a des cellules fusionnées dans le tableau Word
'--- ==> parcourir les cellules du tableau Word une à une
'--- attention: la présentation Excel ne va pas respecter les fusions faites dans Word
Set wdCell = .Cell(1, 1)
Do
iRow = wdCell.RowIndex
iCol = wdCell.ColumnIndex
s = wdCell.Range.Text
s = Replace(s, Chr(11), "|") '--- nouvelle ligne
s = Replace(s, Chr(13), "|") '--- nouveau paragraphe
s = WorksheetFunction.Clean(s) '--- supprime caractères non imprimables (dont Chr(7))
If Right(s, 1) = "|" Then s = Left(s, Len(s) - 1)
s = Replace(s, "|", vbCrLf) '--- remet des sauts de ligne tels qu'admis dans Excel
Cells(kRow + iRow, iCol + 1) = s
Set wdCell = wdCell.Next '--- cellule suivante
Loop Until wdCell Is Nothing
End With
kRow = kRow + iRow + 2
Next tableNo
End If
.Close SaveChanges:=False
End With
strFile = Dir() '--- fichier suivant dans le dossier (ne va pas dans les sous-dossiers)
Wend
'---
wdApp.Quit
Set wdDoc = Nothing
Set wdApp = Nothing
Set WkSht = Nothing
Set WkBk = Nothing
Application.ScreenUpdating = True
Debug.Print "--- Terminé ---"
End Sub
Function GetFolder() As String
Dim oFolder As Object
GetFolder = ""
Set oFolder = CreateObject("Shell.Application").BrowseForFolder(0, "Choose a folder", 0)
If (Not oFolder Is Nothing) Then GetFolder = oFolder.Items.Item.Path
Set oFolder = Nothing
End Function |
Partager