Hey,
I really would appreaciate if someone could help me with following problem.
I have a macro which opens one word document and puts the 4th table into a excel sheet. I would like to add 2 functions.
1. I have about 200 Word documents, i would like to open. Doing this with macro would save a lot of manual effort.
Therefore i found the
. But this allows me only to select multiple files. Something is missing ...
2. the table is not always the 4th in the documents. Sometimes it is the 5th or the 6th so i can't fix it via counting the tables.... . I found the
function. Is it possible to search the Table which starts with "übersicht" an select it to copy?
Thanks alot for help.
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 (*.docx;*doc),*.docx", , _
"Browse for file containing table to be imported", MulitSelect: = True)
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", "4")
End If
resultRow = 1
For tableStart = 4 To 4 '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
Bookmarks