tshipman44
New Member
- Joined
- Jul 27, 2011
- Messages
- 2
I have a VB macro to extract data from a Word Doc which contains a Table that I built based on one listed here. However, I have several thousand Word Docs (each with just one table), that I need to capture data from. It would be much faster to process a batch of them.
Can one of the VB Experts help with modifying the code below to process all the documents ina folder?
Thanks in advance.
Can one of the VB Experts help with modifying the code below to process all the documents ina folder?
Thanks in advance.
Code:
Sub ImportWordTables()
'Imports cells (3,2) and (4,2) from Word document Tables 1-10
Dim wdDoc As Object
Dim wdFileName As Variant
Dim TableNo As Integer 'number of tables in Word doc
Dim iTable As Integer 'table number index
Dim iRow As Long 'row index in Excel
Dim iCol As Integer 'column index in Excel
Dim ix As Long
ix = ActiveSheet.UsedRange.Row - 1 + ActiveSheet.UsedRange.Rows.Count
LastRow = ix
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"
'Else TableNo is actual number of tables between 1 and 9
End If
For iTable = 1 To TableNo
With .tables(iTable)
'copy cell contents from Word table cells to Excel cells in column B and C
Cells(ix + 1, "A") = WorksheetFunction.Clean(.Cell(1, 2))
Cells(ix + 1, "B") = WorksheetFunction.Clean(.Cell(2, 2))
Cells(ix + 1, "C") = WorksheetFunction.Clean(.Cell(3, 2))
Cells(ix + 1, "D") = WorksheetFunction.Clean(.Cell(4, 2))
Cells(ix + 1, "E") = WorksheetFunction.Clean(.Cell(5, 2))
Cells(ix + 1, "F") = WorksheetFunction.Clean(.Cell(6, 2))
Cells(ix + 1, "G") = WorksheetFunction.Clean(.Cell(6, 3))
Cells(ix + 1, "H") = WorksheetFunction.Clean(.Cell(7, 2))
Cells(ix + 1, "I") = WorksheetFunction.Clean(.Cell(8, 2))
Cells(ix + 1, "J") = WorksheetFunction.Clean(.Cell(9, 2))
Cells(ix + 1, "K") = WorksheetFunction.Clean(.Cell(10, 2))
Cells(ix + 1, "L") = WorksheetFunction.Clean(.Cell(13, 2))
End With
Next iTable
End With
Set wdDoc = Nothing
End Sub