Hello, a little help with this code, please. This macro copies data from a single Word file table into a worksheet of Excel. It prompts you for the word document as well as the table number if Word contains more than one table (not my code, taken from StackOverflow).
Problem is: I have hundreds of the same Word file and I need to import them all. The macro should ask only once the number of the table to import (because the type of Word file are all the same) and then import them all in a single sheet.
For example: the macro ask me to select a file, I choose 10 files in a folder. It asks me the number of table and I answer "2". Then it imports the second table of each Word in the same Excel sheet.
Is it possible? Thank you very much.
Magdy
Sub ImportWordTable()
Dim wdDoc As Object
Dim wdFileName As Variant
Dim TableNo As Integer 'table number in Word
Dim iRow As Long 'row index in Excel
Dim iCol As Integer 'column index in Excel
wdFileName = Application.GetOpenFilename("Word files (*.doc),*.doc", , _
"Browse for file containing table to be imported")
If wdFileName = False Then Exit Sub '(user cancelled import file browser)
Set wdDoc = GetObject(wdFileName) 'open Word file
With wdDoc
TableNo = wdDoc.tables.Count
If TableNo = 0 Then
MsgBox "This document contains no tables", _
vbExclamation, "Import Word Table"
ElseIf TableNo > 1 Then
TableNo = InputBox("This Word document contains " & TableNo & " tables." & vbCrLf & _
"Enter table number of table to import", "Import Word Table", "1")
End If
With .tables(TableNo)
'copy cell contents from Word table cells to Excel cells
For iRow = 1 To .Rows.Count
For iCol = 1 To .Columns.Count
Cells(iRow, iCol) = WorksheetFunction.Clean(.cell(iRow, iCol).Range.Text)
Next iCol
Next iRow
End With
End With
Set wdDoc = Nothing
End Sub
Bookmarks