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
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