Copy Word tables to Excel

Loin75

Active Member
Joined
Oct 21, 2009
Messages
281
Hi, the code below has been banding around the internet and is supposed to help copy Word tables into Excel, but I am having trouble with it as I get the message "The requested member of the collection does not exist".

The display box says I have 26 tables in the chosen word document, where would I like to start. No matter which number i enter, I get the same error.

Can someone please help? Thanks

Code:
Sub ImportWordTable2()Dim wdDoc As Object
Dim wdFileName As Variant
Dim tableNo As Integer
Dim iRow As Long
Dim iCol As Integer
Dim resultRow As Long
Dim tableStart As Integer
Dim tableTot As Integer
'On Error Resume Next
'ActiveSheet.Range("A:AZ").ClearContents
wdFileName = Application.GetOpenFilename("Word files (*.docx),*.docx", , _
"Browse for O&M Requirement Tables 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
tableTot = 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 the table number to start from", "Import Word Table", "1")
End If
resultRow = 2
For tableStart = tableNo To tableTot
With .tables(tableStart)
'copy cell contents from Word table cells to Excel cells
For iRow = 1 To .Rows.Count
For iCol = 1 To .Columns.Count
Worksheets(tableStart).Cells(resultRow, iCol) = _
 WorksheetFunction.Clean(.cell(iRow, iCol).Range.Text)
Next iCol
resultRow = resultRow + 1
Next iRow
End With
resultRow = 2
Next tableStart
End With
End Sub
 
Cheers indeed!
That fixed it! - Thanks - I love how it works -- shows the name of each table and details about the table!
Now I've got to go seek out a way to extract a specific table from all the tables in the sheet using a MsgBox...Haven't seen a post with that capability yet, so I'll post a new one now..
Thanks again for your help Koen!
 
Upvote 0

Excel Facts

How can you automate Excel?
Press Alt+F11 from Windows Excel to open the Visual Basic for Applications (VBA) editor.

Forum statistics

Threads
1,223,798
Messages
6,174,667
Members
452,576
Latest member
AlexG_UK

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top