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
|
Option Explicit
Public LigneEnCours As Long, ColonneEnCours As Long
Sub ImporterDesTableWordVersExcel()
'Dim WordApp As Object
'Dim WordDoc As Object
'Dim Tableau As Object
'Nb : Pendant la phase de test, il vaut mieux travailler avec la référence Word cochée
Dim WordApp As Word.Application
Dim WordDoc As Word.Document
Dim Tableau As Word.Table
Dim I As Integer, J As Integer
Dim ShCible As Worksheet
Dim CelluleCible As Range
Dim CheminComplet As String
CheminComplet = "C:\Users\...\test.docx" ' A adapter
Set ShCible = ActiveSheet
With ShCible
' .UsedRange.Clear ' Attention, on efface tout
LigneEnCours = .UsedRange.SpecialCells(xlCellTypeLastCell).Row
ColonneEnCours = 1
Set CelluleCible = .Range("A" & LigneEnCours)
End With
Set WordApp = CreateObject("Word.Application")
With WordApp
.Visible = True
Set WordDoc = .Documents.Open(CheminComplet)
With WordDoc
For I = 1 To .Tables.Count
Set Tableau = .Tables(I)
RecupererLeContenuDesCellulesWord CelluleCible, Tableau
Set Tableau = Nothing
Next I
.Close False
End With
Set WordDoc = Nothing
.Quit
End With
Set WordApp = Nothing
With ShCible
With .UsedRange
.EntireColumn.ColumnWidth = 110
.Replace What:="", Replacement:="", LookAt:=xlPart, _
SearchOrder:=xlByRows, MatchCase:=False, SearchFormat:=False, ReplaceFormat:=False
.Columns.AutoFit
.Rows.AutoFit
End With
End With
Set ShCible = Nothing
MsgBox "Import terminé !", vbInformation
End Sub
Sub RecupererLeContenuDesCellulesWord(ByVal CelluleEnCours As Range, ByVal TableEnCours As Word.Table)
Dim NombreDeColonnesWord As Long, NombreDeLignesWord As Long, ColonneWord As Long, LigneWord As Long
Dim J As Integer
Dim MonRangeWord As Word.Range
Dim MonTexte As String
With TableEnCours
NombreDeLignesWord = .Rows.Count
For LigneWord = 1 To NombreDeLignesWord
NombreDeColonnesWord = .Rows(LigneWord).Cells.Count
For ColonneWord = 1 To NombreDeColonnesWord
Set MonRangeWord = .Cell(LigneWord, ColonneWord).Range
With MonRangeWord
MonTexte = ""
For J = 1 To .Paragraphs.Count
MonTexte = MonTexte & .Paragraphs(J).Range.Text & Chr(10)
Next J
End With
CelluleEnCours.Offset(LigneEnCours, ColonneEnCours + ColonneWord) = MonTexte
'Debug.Print NombreDeLignesWord & ", " & NombreDeColonnesWord & " : " & MonTexte
Set MonRangeWord = Nothing
Next ColonneWord
LigneEnCours = LigneEnCours + 1
Next LigneWord
End With
End Sub |
Partager