Bonjour,
Créé une class et colles-y le code suivant :
Imports Microsoft.Office.Interop
Public Class msWord
Public oWord As Microsoft.Office.Interop.Word.Application
Public oDoc As Microsoft.Office.Interop.Word.Document
Public Function openDoc(ByRef strDoc) As Object
oWord = CreateObject("Word.Application")
oWord.Visible = True
oDoc = oWord.Documents.Open(strDoc)
Return oWord
End Function
Public Function getTables() As Object
Dim Tbl() As Microsoft.Office.Interop.Word.Table
Dim i As Long
Dim nbTbl As Long
'Nombre de tableaux dans le document
nbTbl = oDoc.Tables.Count
ReDim Preserve Tbl(nbTbl - 1)
For i = 1 To nbTbl
Tbl(i - 1) = oDoc.Tables(i)
Next
Return Tbl
End Function
Public Function getValCell(ByRef Cellule As Object) As String
Dim valeurCellule As String = ""
valeurCellule = Cellule.Range.Text.ToString
Return valeurCellule
End Function
Public Function docClose() As Boolean
oDoc.Close()
oDoc = Nothing
Return True
End Function
Public Function getTbltoArray(ByRef Table) As Array
Dim Cell As Object
Dim valCell As String
Dim nbRows As Long = Table.Rows.Count
Dim nbCols As Long = Table.Columns.Count
Dim arrResult(nbRows, nbCols) As String
'Pour chaque ligne du tableau
For i = 1 To nbRows
'Pour chaque colonne...
For j = 1 To nbCols
Cell = Table.Columns(j).Cells(i)
valCell = getValCell(Cell)
arrResult(i, j) = valCell
Next
Next
Return arrResult
End Function
End Class
Ne pas oublier d'ajouter la référence à word ..
Add a reference to the Microsoft Word Object Library. To do this, follow these steps:
On the Project menu, click Add Reference.
On the COM tab, locate the Microsoft Word Object Library and click Select.
Dans ton code ( par exemple je l'ai mis dans un bouton d'un FORM )
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
Dim fichier As String = "c:\tmp\test.docx"
Dim wordClass As New msWord
Dim Doc As Object
Dim tables As Object
Doc = wordClass.openDoc(fichier)
tables = wordClass.getTables()
For Each Tableau In tables 'pour chaque tableau du document
Dim arrTbl As Array = wordClass.getTbltoArray(Tableau)
For i = 1 To UBound(arrTbl)
Console.WriteLine("----------------------")
For j = 1 To UBound(arrTbl, 2)
Console.Write(arrTbl(i, j))
Next
Next
Next
'fermeture du classeur
wordClass.docClose()
End Sub
Bien entendu .. je n'ai fais qu'afficher le contenu du tableau...
A toi de coder l'enregistrement en BDD !
EDIT : Petite amélioration de la class.
Cordialement,
Jordane