I have 100+ Word documents that I need to import into Excel
The code below from this recent thread works perfectly with the format of the Word docs we are using.
Would it be possible to modify the code to choose the folder containing the Word docs, and then import the content of each document on a new worksheet? (and name the worksheet the same name as the Word doc?)
The code below from this recent thread works perfectly with the format of the Word docs we are using.
Import multiple Word tables into Excel using VBA - Retaining some original Word formatting
Hi I am trying to extract and import a number of tables from a word document (this number can vary between documents) There are normally only 2 columns of information (but on occasion can be more) I have the following code which will extract and import all the tables into my excel document...
www.mrexcel.com
Would it be possible to modify the code to choose the folder containing the Word docs, and then import the content of each document on a new worksheet? (and name the worksheet the same name as the Word doc?)
VBA 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("A4: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