![]() |
|
#8
|
||||
|
||||
|
Try the following macro. It allows the user to select a single new form to use as a template for the output files, then an output folder, then all the source files from which the data are to be transferred. A new output document is created in the output folder with the same name as each of the source files. Output files are saved in the .docx format.
Code:
Sub DataReplication()
Dim NewFile As Variant, OldFile As Variant, StrFoldr As String
Dim DocSrc As Document, DocTgt As Document, FmFld As FormField
With Application.FileDialog(msoFileDialogFilePicker)
.Title = "Please select the new form"
.AllowMultiSelect = False
.Filters.Add "Documents & Templates", "*.doc*; *.docx; *.dot*", 1
'use Show method to display File Picker dialog box and return user's action
If .Show = -1 Then
NewFile = .SelectedItems(1)
Else
Exit Sub
End If
End With
StrFoldr = GetFolder(Title:="Select the Destination Folder", RootFolder:="C:\Users\" & Environ("UserName"))
If StrFoldr = "" Then Exit Sub
With Application.FileDialog(msoFileDialogFilePicker)
.Title = "Please select the old documents"
.AllowMultiSelect = True
.Filters.Add "Documents", "*.doc; *.docx", 1
'use Show method to display File Picker dialog box and return user's action
If .Show = -1 Then
'step through each item in the collection
For Each OldFile In .SelectedItems
Set DocTgt = Documents.Add(NewFile, Visible:=False)
Set DocSrc = Documents.Open(OldFile, AddToRecentFiles:=False, Visible:=False)
With DocTgt
For Each FmFld In .FormFields
FmFld.Result = DocSrc.FormFields(FmFld.Name).Result
Next
.SaveAs StrFoldr & "\" & Split(DocSrc.Name, ".")(0) & ".docx", FileFormat:=wdFormatXMLDocument, AddToRecentFiles:=False
.Close False
End With
DocSrc.Close False
Next
Else
Exit Sub
End If
End With
End Sub
__________________
Cheers, Paul Edstein [Fmr MS MVP - Word] |
|
|
Similar Threads
|
||||
| Thread | Thread Starter | Forum | Replies | Last Post |
| importing Access data to a Word document | WayneCusack | Word VBA | 2 | 12-24-2012 12:26 AM |
| Print word form using excel data sheet | LS1015 | Office | 1 | 07-16-2012 08:16 PM |
| word form data | Jasafras2 | Word VBA | 1 | 05-27-2011 03:12 PM |
| *Big One* feeding sql server data into MS Word 2007 forms | vafo | Mail Merge | 0 | 02-03-2010 04:49 AM |
Forms - linked form boxes
|
m.beharry | Word | 2 | 07-03-2009 09:12 AM |