Microsoft Office Forums

Go Back   Microsoft Office Forums > >

Reply
 
Thread Tools Display Modes
  #1  
Old 03-24-2014, 01:12 AM
adisl adisl is offline Word macro: copy from different tables into one table Windows 7 64bit Word macro: copy from different tables into one table Office 2007
Novice
Word macro: copy from different tables into one table
 
Join Date: Mar 2014
Posts: 7
adisl is on a distinguished road
Default Word macro: copy from different tables into one table

Does anyone have an idea how this can be solved.
I need to copy data from tables that are located in several different documents and folders and compile them into one table in a main document.
* The tables will have a different number of rows but have the same number of columns.
* Only to copy the rows that have yes to second column (if possible)
Is deeply grateful if someone can send sample code for this


Example below should present tables
From document 1
ID1 | yes | Text
ID2 | no | Text

From dokument 2
ID3|yes|Text
ID4|yes|Text
ID5|no |Text

From document 3
ID6|no |text
ID7|yes|text

main document to be copied to
ID1|Yes|text
ID3|yes|text
ID4|yes|text
ID7|yes|text




Sincerely Svein

Last edited by adisl; 03-24-2014 at 03:18 AM. Reason: presentation was poor
Reply With Quote
  #2  
Old 03-24-2014, 08:45 PM
macropod's Avatar
macropod macropod is offline Word macro: copy from different tables into one table Windows 7 32bit Word macro: copy from different tables into one table Office 2010 32bit
Administrator
 
Join Date: Dec 2010
Location: Canberra, Australia
Posts: 21,963
macropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond repute
Default

How many columns do these tables have?
__________________
Cheers,
Paul Edstein
[Fmr MS MVP - Word]
Reply With Quote
  #3  
Old 03-24-2014, 11:55 PM
adisl adisl is offline Word macro: copy from different tables into one table Windows 7 64bit Word macro: copy from different tables into one table Office 2007
Novice
Word macro: copy from different tables into one table
 
Join Date: Mar 2014
Posts: 7
adisl is on a distinguished road
Default

They have four columns
Reply With Quote
  #4  
Old 03-25-2014, 01:22 AM
macropod's Avatar
macropod macropod is offline Word macro: copy from different tables into one table Windows 7 32bit Word macro: copy from different tables into one table Office 2010 32bit
Administrator
 
Join Date: Dec 2010
Location: Canberra, Australia
Posts: 21,963
macropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond repute
Default

Try the following. It should work with any number of columns. Although there's nothing in the code to ensure all rows end up being the same width, that won't matter if they are in fact the same width. The macro you run is the top one: "Main"; it includes its own folder browser and will process all files in the selected folder and the folders below it. It also outputs a report of any files it can't process because of protection and the like.
Code:
Option Explicit
Public FSO As Object 'a FileSystemObject
Public oFolder As Object 'the folder object
Public oSubFolder As Object 'the subfolders collection
Public oFiles As Object 'the files object
Public i As Long, j As Long
Public DocTgt As Document
 
Sub Main()
' Minimise screen flickering
Application.ScreenUpdating = False
Dim StrFolder As String
' Browse for the starting folder
StrFolder = GetTopFolder
If StrFolder = "" Then Exit Sub
' Initialize the counters
i = 0: j = 0
' Search the top-level folder
Set DocTgt = Documents.Add()
Call GetFolder(StrFolder & "\")
' Search the subfolders for more files
Call SearchSubFolders(StrFolder)
' Return control of status bar to Word
Application.StatusBar = ""
' Restore screen updating
Application.ScreenUpdating = True
MsgBox i & " of " & j & " files processed.", vbOKOnly
End Sub
 
Function GetTopFolder() As String
GetTopFolder = ""
Set oFolder = CreateObject("Shell.Application").BrowseForFolder(0, "Choose a folder", 0)
If (Not oFolder Is Nothing) Then GetTopFolder = oFolder.Items.Item.Path
Set oFolder = Nothing
End Function
 
Sub SearchSubFolders(strStartPath As String)
If FSO Is Nothing Then
  Set FSO = CreateObject("scripting.filesystemobject")
End If
Set oFolder = FSO.GetFolder(strStartPath)
Set oSubFolder = oFolder.subfolders
For Each oFolder In oSubFolder
  Set oFiles = oFolder.Files
  ' Search the current folder
  Call GetFolder(oFolder.Path & "\")
  ' Call ourself to see if there are subfolders below
  SearchSubFolders oFolder.Path
Next
End Sub
 
Sub GetFolder(StrFolder As String)
Dim strFile As String
strFile = Dir(StrFolder & "*.doc")
' Process the files in the folder
While strFile <> ""
  ' Update the status bar is just to let us know where we are
  Application.StatusBar = StrFolder & strFile
  Call UpdateFile(StrFolder & strFile)
  strFile = Dir()
Wend
End Sub
 
Sub UpdateFile(strDoc As String)
Dim Doc As Document
' Open the document
Set Doc = Documents.Open(strDoc, AddToRecentFiles:=False, ReadOnly:=False, Format:=wdOpenFormatAuto, Visible:=False)
With Doc
  If .ProtectionType = wdNoProtection Then
    Call GetTableData(Doc)
    ' Update the file counter for processed files
    i = i + 1
  Else
    ' Output a 'protected' file report in the document from which the macro is run.
    ThisDocument.Range.InsertAfter vbCr & strDoc & " protected. Not updated."
  End If
  ' Update the main file counter
  j = j + 1
  .Close SaveChanges:=False
End With
' Let Word do its housekeeping
DoEvents
Set Doc = Nothing
End Sub
 
Sub GetTableData(Doc As Document)
Dim Tbl As Table, i As Long, Rng As Range
With Doc
  For Each Tbl In .Tables
    For i = 1 To Tbl.Rows.Count
      Set Rng = Tbl.Cell(i, 2).Range
      Rng.End = Rng.End - 1
      If Rng.Text = "Yes" Then
        Set Rng = Tbl.Rows(i).Range
        DocTgt.Characters.Last.FormattedText = Rng.FormattedText
      End If
    Next
  Next
End With
End Sub
__________________
Cheers,
Paul Edstein
[Fmr MS MVP - Word]
Reply With Quote
  #5  
Old 03-25-2014, 02:40 AM
adisl adisl is offline Word macro: copy from different tables into one table Windows 7 64bit Word macro: copy from different tables into one table Office 2007
Novice
Word macro: copy from different tables into one table
 
Join Date: Mar 2014
Posts: 7
adisl is on a distinguished road
Default

fantastic just what I was looking for.
Thanks so much
Reply With Quote
Reply



Similar Threads
Thread Thread Starter Forum Replies Last Post
Copy Excel data into an existing Word Table JJG Word 1 12-18-2013 05:41 PM
Word macro: copy from different tables into one table Cannot Copy and Paste Word 2010 Tables jctech1943 Word 8 07-03-2012 04:16 AM
how to copy all ms word tables into excel rehan129 Word 0 01-28-2012 10:17 AM
Word macro: copy from different tables into one table Copy table cell formatting across multiple cells / tables pakistanray Word Tables 2 10-31-2011 08:07 AM
Word macro: copy from different tables into one table Word Macro to search all tables silverspr Word VBA 3 04-02-2011 11:20 PM

Other Forums: Access Forums

All times are GMT -7. The time now is 09:20 AM.


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