Results 1 to 1 of 1

Copy table content from Word with condition of a certain cell to excel workbook

Threaded View

Exvbassist Copy table content from Word... 03-09-2014, 08:12 AM
  1. #1
    Registered User
    Join Date
    03-08-2014
    Location
    Zürich
    MS-Off Ver
    Excel 2010
    Posts
    1

    Copy table content from Word with condition of a certain cell to excel workbook

    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
    Last edited by Exvbassist; 03-11-2014 at 08:03 AM.

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Similar Threads

  1. [SOLVED] Can I copy a table from Word into one cell in Excel without losing data? (Of
    By memoto@myself.com in forum Excel General
    Replies: 4
    Last Post: 09-06-2016, 03:23 PM
  2. [SOLVED] Copy from excel to a word table to the next available cell in column
    By stojko89 in forum Excel Programming / VBA / Macros
    Replies: 3
    Last Post: 10-17-2012, 01:32 AM
  3. check each cell for certain condition, copy the content in a new worksheet
    By cyin in forum Excel Programming / VBA / Macros
    Replies: 2
    Last Post: 04-21-2012, 09:49 PM
  4. copy partial cell content (with condition)
    By soak614 in forum Excel General
    Replies: 3
    Last Post: 06-26-2010, 05:11 AM
  5. Replies: 3
    Last Post: 06-10-2008, 09:20 AM

Tags for this Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts

Search Engine Friendly URLs by vBSEO 3.6.0 RC 1