OK let's see how fast
The following is one possible solution.
The macro shown on my web page
http://www.gmayor.com/ExtractDataFromForms.htm explains how to extract the
data from a folder full of forms to a comma delimited text file. It is
fairly simply to modify that to work with a single form document as follows.
The macro extracts the data from the selected form and adds it to the text
document (see the above link).
Additional code then creates the report from your pre-prepared report
template 'Report.dot'.
eg
{ DocVariable"varFirstName" } { DocVariable"varLastName" }
{ DocVariable"varDate" }
Dear { DocVariable"varFirstName" }
You have { DocVariable"varWidget" } widgets to go with {
DocVariable"varWhatsit" } whatsits, but still require {
DocVariable"varThingummy" } more { IF { DocVariable"varThingummy" } = 1
"thingummy" "thingummies" }
Yours sincerely
All you have to do is arrange the variables to match your original form.
Sub PrepareReport()
Dim DocList As String
Dim DocDir As String
Dim FormDoc As Document
Dim DataDoc As Document
Dim TargetDoc As Document
Dim NewDoc As Document
Dim fDialog As FileDialog
Dim oRng As Range
Dim sText As String
Dim i As Long
Dim sFormData() As String
Dim oVars As Variables
Set fDialog = Application.FileDialog(msoFileDialogFilePicker)
On Error GoTo err_FolderContents
With fDialog
.Title = "Select The completed form document and click OK"
.AllowMultiSelect = False
.InitialView = msoFileDialogViewList
If .Show <> -1 Then
MsgBox "Cancelled By User"
Exit Sub
End If
DocDir = fDialog.SelectedItems.Item(1)
End With
If Documents.Count > 0 Then
Documents.Close SaveChanges:=wdPromptToSaveChanges
End If
Application.ScreenUpdating = False
Set FormDoc = Documents.Open(DocDir)
With FormDoc
Set TargetDoc = Documents.Open("TargetDoc.txt", False)
.SaveFormsData = True
.SaveAs FileName:="DataDoc.txt", _
FileFormat:=wdFormatText, _
SaveFormsData:=True
.Close SaveChanges:=wdDoNotSaveChanges
Set DataDoc = Documents.Open("DataDoc.txt", False)
End With
TargetDoc.Range.InsertAfter DataDoc.Range
DataDoc.Close SaveChanges:=wdDoNotSaveChanges
TargetDoc.Save
Set oRng = TargetDoc.Paragraphs(TargetDoc.Paragraphs.Count - 1).Range
oRng.End = oRng.End - 1
sFormData = Split(oRng.Text, ",")
Set NewDoc = Documents.Add(Options.DefaultFilePath(wdUserTemplatesPath) &
"\Report.dot")
Set oVars = NewDoc.Variables
oVars("varFirstName").Value = Replace(sFormData(0), Chr(34), "")
oVars("varLastName").Value = Replace(sFormData(1), Chr(34), "")
oVars("varDate").Value = Replace(sFormData(2), Chr(34), "")
oVars("varWidget").Value = Replace(sFormData(3), Chr(34), "")
oVars("varWhatsit").Value = Replace(sFormData(4), Chr(34), "")
oVars("varThingummy").Value = Replace(sFormData(5), Chr(34), "")
For i = NewDoc.Fields.Count To 1 Step -1
With NewDoc.Fields(i)
If .Type = wdFieldDocVariable Then .Update
End With
Next i
TargetDoc.Close wdDoNotSaveChanges
Application.ScreenUpdating = True
'NewDoc.Save
Exit Sub
err_FolderContents:
MsgBox Err.Description
End Sub
--
<>>< ><<> ><<> <>>< ><<> <>>< <>><<>
Graham Mayor - Word MVP
<>>< ><<> ><<> <>>< ><<> <>>< <>><<>