Extract Copy All Tables from Word into Excel

ChrisOK

Well-known Member
Joined
Mar 26, 2003
Messages
601
I've got some code that successfully extracts all the tables from within a Word doc - over into Excel - however, I'm wondering if anyone knows of a way to make it highlight/color/or draw a bold dark line between each table as it is pasting them downward in sequence.
(need to be able to easily scroll down and see where a new table starts - so drawing that line - or turning that top row a color would be awesome if doable?)

Here's what I'm using now / but totally open to something different/better:
Code:
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
 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 (*.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
 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 to start from", "Import Word Table", "1")
 End If

 resultRow = 4

 For tableStart = 1 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
 Cells(resultRow, iCol) = WorksheetFunction.Clean(.Cell(iRow, iCol).Range.Text)
 Next iCol
 resultRow = resultRow + 1
 Next iRow
 End With
 resultRow = resultRow + 1
 Next tableStart
 End With

 End Sub
 

Excel Facts

How to show all formulas in Excel?
Press Ctrl+` to show all formulas. Press it again to toggle back to numbers. The grave accent is often under the tilde on US keyboards.
Try:
Code:
Sub ImportWordTable()

Dim wdDoc As Object
Dim wdFileName As Variant
Dim tableNo As Long 'table number in Word
Dim iRow As Long 'row index in Excel
Dim iCol As Long 'column index in Excel
Dim resultRow As Long
Dim tableStart As Long
Dim tableTot As Long
Dim wkSht As Worksheet

On Error Resume Next

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 wkSht = ActiveSheet
  wkSht.Range("A:AZ").ClearContents

  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 to start from", "Import Word Table", "1")
    End If

    resultRow = 4

    For tableStart = 1 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
            wkSht.Cells(resultRow, iCol) = WorksheetFunction.Clean(.Cell(iRow, iCol).Range.Text)
          Next iCol
          resultRow = resultRow + 1
        Next iRow
      End With
      resultRow = resultRow + 1
      With wkSht
        .Range(.Cells(resultRow, 1), .Cells(resultRow, iCol)).Interior.ColorIndex = 15
      End With
      resultRow = resultRow + 1
    Next tableStart
End With

End Sub
 
Upvote 0
@Macropod @ChrisOK

Do you have code to extract specific tables from word to excel. user should be able to select the tables they want to extract from word to excel. any help on this please.
 
Upvote 0

Forum statistics

Threads
1,223,639
Messages
6,173,498
Members
452,516
Latest member
druck21

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