![]() |
|
#1
|
|||
|
|||
![]()
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 |
#2
|
||||
|
||||
![]()
How many columns do these tables have?
__________________
Cheers, Paul Edstein [Fmr MS MVP - Word] |
#3
|
|||
|
|||
![]()
They have four columns
|
#4
|
||||
|
||||
![]()
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] |
#5
|
|||
|
|||
![]()
fantastic just what I was looking for.
Thanks so much ![]() |
![]() |
|
![]() |
||||
Thread | Thread Starter | Forum | Replies | Last Post |
Copy Excel data into an existing Word Table | JJG | Word | 1 | 12-18-2013 05:41 PM |
![]() |
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 |
![]() |
pakistanray | Word Tables | 2 | 10-31-2011 08:07 AM |
![]() |
silverspr | Word VBA | 3 | 04-02-2011 11:20 PM |