HI,
I would like to ask for some help on the following issue I could not solve myself. I would need a VBA code to get data out of multiple tables in word docx and copied to excel. However, I have tables in the document that I do not need data from so I need to be able to select all those tables that apply without manually checking the files. (we are talking about hundreds of word documents with 5-8 tables out of 12 tabels in each)
in short:
- I have all the document files in a specific folder (master_folder)
- I have to open them one by one and copy the data from the tables that meet given criteria. This is how my tables generally look like in the document.
ID |
What |
Finding |
Nom. |
Acceptance |
Rejected |
1 |
something |
empty |
12345 |
as is |
NO |
1.1 |
anything |
full |
35262GF |
modify |
YES |
1.2 |
nothing |
half full |
HGT654G2H |
as is |
NO |
From the multiple tables in each document I am only interested in those that have one thing in common which is the header (or the first row) of the tables. The first cell of the first row in each table (I am interested in) has a value "ID". I would need to copy all the info from these tables starting from the second row until the last row of the table and from each and every table that has the first cell value as "ID" in the document.
The excel file will have the same header so I do not need to copy the headers (or first rows). Also, since each table have the same header copying them multiple time is not needed and would cause additional cleaning process in the master file each time I update it.
I also would like that once the data had been copied from the file the program moves the files to an other folder (master_DONE) so I know what is done and what is not.
The master file would be updated multiple times as new documents are available so I need to make sure that already collected data is not deleted or overwritten while processing new documents.
Please let me know if there is any further clarification needed to understand what I would like to achive.
Thank you for any possible help or a full VBA code would be higly appreciated to get this done.
Cheers,
Ex
After trying hard..
this is what I came up with but it is just a tiny bit of what I actually want. My knowledge ends here pretty much. Please, anyone! Any help, check this for me and let me know how can I make this actually work the way I need it.
What I could achieve is:
- it opens .docx files when I show it
- counts the tables in the document
- asks which table I want to start with (because I could not get it checking for the certain tables as described above and I know the usualy I need tables from the 2nd. But the program should be able to pick tabels based on above criteria)
- copy the tables starting from the table user says
- and it repeats (the questions and counting as well) it as many times as many .docx files are in the folder
- it starts copying from the second row of each table because that is what I need the info from
problems I still need to solve:
- it should automatically find the files in the folder (pre-set in the program) and specified tabels in the documents, then copy/paste
- automatically go through all the files in the folder that has specified extension (.docx)
- paste info to the first available empty row. At this point, it unfortunately overwrites the data already pasted...(do not know how to tell exactly that it should start pasting to the first empty row)
Sub ImportWordTables()
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
'loopThrough_folder first part
Dim MyFile As String
Dim erow
Dim Filepath As String
Filepath = "D:\master_folder\"
MyFile = Dir(Filepath)
Do While Len(MyFile) > 0
If MyFile = "zWordTest.xlsm" Then
Exit Sub
End If
'loopThrough folder first part end
On Error Resume Next
wdFileName = Application.GetOpenFilename("Word files (*.docx),*.docx", , _
"Browse for file containing table to be imported")
If wdFileName = False Then Exit Sub
Set wdDoc = GetObject(wdFileName)
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)
For iRow = 2 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
Next tableStart
With .tables(tableNo)
For iRow = 2 To .Rows.Count
For iCol = 1 To .Columns.Count
Cells(iRow, iCol) = WorksheetFunction.Clean(.cell(iRow, iCol).Range.Text)
Next iCol
Next iRow
End With
End With
MyFile = Dir
Loop
End Sub
Please, let me know if what I want is actually possible.
Thanks in advance!
Ex
Bookmarks