VB Code to Copy MS Word Tables Into Excel

dallin01

Board Regular
Joined
Sep 16, 2009
Messages
61
We use MSWord to prepare the Company's Form 10k -- within this document are at least 25 tables containing financial data. Quarterly we copy the MSWord Tables into MSExcel to test data. I would like to create a macro that goes out into the MSWord document and copy the Tables into MsExcel worksheet cell locations. Can someone give me a reference or provide sample code on how to call MSWord tables and copy them into MSExcel worksheet cell locations?

Thanks!
 

Excel Facts

Excel Joke
Why can't spreadsheets drive cars? They crash too often!
If you prefer to drive the process from the Excel end, you might like to try this code. It's plagiarised from Andrew's link and modified to work from Excel:-
Code:
[FONT=Courier New][SIZE=1]Option Explicit[/SIZE][/FONT]
[FONT=Courier New][SIZE=1][/SIZE][/FONT] 
[FONT=Courier New][SIZE=1]Sub FetchWordTables()[/SIZE][/FONT]
[FONT=Courier New][SIZE=1][/SIZE][/FONT] 
[FONT=Courier New][SIZE=1]  Dim wordApp As Word.Application
  Dim wordDoc As Word.Document
  Dim oTbls As Word.Tables
  Dim oTbl As Word.Table
  Dim ws As Worksheet
  
  Dim sFileName As String
  
  Dim rTable As Range
  Dim iRows As Long
  Dim iNextFreeRow As Long
  Dim iTables As Long
  Dim iPtr As Integer
  
  Dim sMessage As String
 
  Set wordApp = CreateObject("Word.Application")
  sFileName = Application.GetOpenFilename(FileFilter:="Word documents (*.doc*), *.doc*")
  If sFileName = "False" Then Exit Sub
  
  Set wordDoc = wordApp.Documents.Open(sFileName)
  wordApp.Visible = False
  Set oTbls = wordDoc.Tables
  
  Set ws = Sheets("[COLOR=red][B]Sheet3[/B][/COLOR]")
  
  iRows = ws.UsedRange.Row + ws.UsedRange.Rows.Count + 1
  ws.Rows("1:" & CStr(iRows)).Delete Shift:=xlUp
 
  iTables = 0
  iNextFreeRow = 1
  
  For Each oTbl In oTbls
    oTbl.Select
    wordApp.Selection.Copy
    Set rTable = ws.Cells(iNextFreeRow, 1)
    rTable.Select
    ActiveSheet.Paste
    sMessage = sMessage & ", " & rTable.Address(False, False) _
             & " (" & CStr(oTbl.Rows.Count) & "x" & oTbl.Columns.Count & ")"
    iTables = iTables + 1
    iRows = oTbl.Rows.Count
    iNextFreeRow = iNextFreeRow + iRows + 1
  Next oTbl
  
  wordApp.Quit savechanges:=False
  
  sMessage = Mid(sMessage, 3)
  iPtr = InStrRev(sMessage, ",")
  If iPtr > 0 Then sMessage = Left(sMessage, iPtr - 1) & " &" & Mid(sMessage, iPtr + 1)
  
  MsgBox "Done: " & CStr(iTables) & " table" & IIf(iTables = 1, "", "s") & " imported into " _
     & sMessage & Space(10), vbOKOnly + vbInformation
  
End Sub[/SIZE][/FONT]
Change the bit in red to point to your worksheet. You'll also need to add a reference to the Microsoft Word Object Library in VBA (Tools > References).
 
Upvote 0
Awesome - Thankyou! Always educational! Is it possible to target a specific Table in word; where can I find in MSword what a table is named so I could target specifice tables?
 
Upvote 0
This allows you to select the table number. Run it with a blank sheet selected:

Code:
Sub ImportWordTable1()
'Import one table to current sheet
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
                On Error Resume Next
                Cells(iRow, iCol) = WorksheetFunction.Clean(.cell(iRow, iCol).Range.Text)
                On Error GoTo 0
            Next iCol
        Next iRow
    End With
End With
Set wdDoc = Nothing
End Sub
 
Upvote 0

Forum statistics

Threads
1,215,108
Messages
6,123,129
Members
449,097
Latest member
mlckr

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