I have a word document:
Table 1
A A
B B
Table 2
C C
D D
I am trying to copy each piece of text from my tables in a word document to cells in excel like so:
Excel:
Column A Column B
A A
B B
C C
D D
The code below is only copying the last table in my word document. Producing this result:
Excel:
Column A Column B
C C
D D
Here's my code:
Sub ImportWordTable()
Dim objWord As Object
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
Set objWord = GetObject(, "Word.Application")
Set wdDoc = objWord.ActiveDocument
With wdDoc
TableNo = wdDoc.tables.Count
If .tables.Count > 0 Then
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 If
End With
Set wdDoc = Nothing
End Sub
Please can someone show me where I am going wrong? I think i need to add a for each loop for TableNo
Something like
For Each TableNo In wdDoc
Next TableNo