Microsoft Office Forums batch extract all tables in multiple word documents

Go Back   Microsoft Office Forums > >

Reply
 
Thread Tools Display Modes
  #1  
Old 05-05-2017, 02:11 AM
ZaidaBa ZaidaBa is offline batch extract all tables in multiple word documents Windows 7 64bit batch extract all tables in multiple word documents Office 2010 32bit
Novice
batch extract all tables in multiple word documents
 
Join Date: May 2017
Posts: 2
ZaidaBa is on a distinguished road
Default batch extract all tables in multiple word documents

Hey,

Can I extract all tables in multiple word documents to a new word document?



I need to get all tables from a batch of word documents to compare and print them. Now I can only copy and paste them to the new document one by one manually. But there are almost 25 files and each file contains a lot of tables. how to do that quickly? Thank you for help.
Reply With Quote
  #2  
Old 05-05-2017, 11:16 PM
gmayor's Avatar
gmayor gmayor is offline batch extract all tables in multiple word documents Windows 10 batch extract all tables in multiple word documents Office 2016
Expert
 
Join Date: Aug 2014
Posts: 2,999
gmayor is just really nicegmayor is just really nicegmayor is just really nicegmayor is just really nicegmayor is just really nice
Default

Maybe something like the following, which also produces a log of the documents processed and the number of tables extracted.

Code:
Option Explicit

Sub BatchCopyTables()
'Graham Mayor - http://www.gmayor.com - Last updated - 06/05/2017
Dim strFileName As String
Dim strPath As String
Dim oDoc As Document, oNewDoc As Document
Dim oTable As Range, oRng As Range
Dim oLog As Document
Dim bFound As Boolean
Dim fDialog As FileDialog
Dim oColl As New Collection
Dim i As Long, j As Long, k As Long

    Set fDialog = Application.FileDialog(msoFileDialogFolderPicker)
    With fDialog
        .Title = "Select folder and click OK"
        .AllowMultiSelect = False
        .InitialView = msoFileDialogViewList
        If .Show <> -1 Then
            MsgBox "Cancelled By User", , _
                   "List Folder Contents"
            GoTo lbl_Exit
        End If
        strPath = fDialog.SelectedItems.Item(1) & Chr(92)
    End With
    Set oNewDoc = Documents.Add

    strFileName = Dir$(strPath & "*.doc")

    While Len(strFileName) <> 0
        Set oDoc = Documents.Open(FileName:=strPath & strFileName, AddtoRecentFiles:=False)
        bFound = False
        If oDoc.ProtectionType = wdNoProtection Then
            If oDoc.Tables.Count > 0 Then
                k = 0
                bFound = True
                For i = 1 To oDoc.Tables.Count
                    Set oTable = oDoc.Tables(i).Range
                    oTable.Copy
                    Set oRng = oNewDoc.Range
                    oRng.Collapse 0
                    oRng.InsertParagraphAfter
                    Set oRng = oNewDoc.Range
                    oRng.Collapse 0
                    oRng.Paste
                    k = k + 1
                    DoEvents
                Next i
                If bFound = True Then
                    oColl.Add strFileName & vbTab & k & " tables copied"
                End If
            End If
            DoEvents
        End If
        oDoc.Close SaveChanges:=wdDoNotSaveChanges
        strFileName = Dir$()
    Wend
    Set oLog = Documents.Add
    For j = 1 To oColl.Count
        oLog.Range.InsertAfter oColl(j) & vbCr
    Next j
lbl_Exit:
    Exit Sub
End Sub
__________________
Graham Mayor - MS MVP (Word) (2002-2019)
Visit my web site for more programming tips and ready made processes www.gmayor.com
Reply With Quote
  #3  
Old 05-07-2017, 11:02 PM
macropod's Avatar
macropod macropod is offline batch extract all tables in multiple word documents Windows 7 64bit batch extract all tables in multiple word documents Office 2010 32bit
Administrator
 
Join Date: Dec 2010
Location: Canberra, Australia
Posts: 19,599
macropod is a splendid one to beholdmacropod is a splendid one to beholdmacropod is a splendid one to beholdmacropod is a splendid one to beholdmacropod is a splendid one to beholdmacropod is a splendid one to beholdmacropod is a splendid one to beholdmacropod is a splendid one to behold
Default

Cross-posted at: https://www.office-forums.com/thread...ments.2349954/
and: https://answers.microsoft.com/en-us/...a-e0609bc253bb
For cross-posting etiquette, please read: http://www.excelguru.ca/content.php?184
__________________
Cheers,
Paul Edstein
[MS MVP - Word]
Reply With Quote
  #4  
Old 05-08-2017, 10:22 PM
ZaidaBa ZaidaBa is offline batch extract all tables in multiple word documents Windows 7 64bit batch extract all tables in multiple word documents Office 2010 32bit
Novice
batch extract all tables in multiple word documents
 
Join Date: May 2017
Posts: 2
ZaidaBa is on a distinguished road
Default

hi,Graham

I'm really appreciated for your help, the macro works well to me !
Reply With Quote
Reply

Thread Tools
Display Modes


Similar Threads
Thread Thread Starter Forum Replies Last Post
batch extract all tables in multiple word documents How to extract sections with the same heading from multiple documents and merge into new single doc edumac Word 2 04-10-2017 04:11 PM
batch extract all tables in multiple word documents Link Tables in multiple documents Stormie Word Tables 4 05-24-2016 06:53 PM
Extract Data from Word Documents hiwire03 Word VBA 3 01-29-2016 09:19 PM
batch extract all tables in multiple word documents Batch create Word documents cdfj Word VBA 6 11-07-2012 01:03 PM
batch extract all tables in multiple word documents VBA code to extract specific bookmarks from multiple word files Rattykins Word VBA 4 06-27-2012 10:02 PM


All times are GMT -7. The time now is 07:24 AM.


Powered by vBulletin® Version 3.8.11
Copyright ©2000 - 2019, vBulletin Solutions Inc.
Search Engine Optimisation provided by DragonByte SEO (Lite) - vBulletin Mods & Addons Copyright © 2019 DragonByte Technologies Ltd.
MSOfficeForums.com is not affiliated with Microsoft