How to transfer data from one MSword doc to another

M

Mdmax

We have a form that new eyeglass patients fill out in our office, it
includes fill in the blank areas for name, address, doctor etc., and
then numerous check boxes for health symptoms, such as headaches,
dizziness, blurred vision, etc.

We take the information from the generic form and manually create a
specific report for that patient, such as patient "smith" complains of
"headaches, dizziness, and blurred vision" (based on the responses from
the form).

The question is how can we populate information in the new report with
data from the generic form with a minimum of editing? (or is that too
much to ask of word?)
 
G

Graham Mayor

M

Mdmax

Hi, thanks for your reply, I've been trying to get advice on several
forums for several days with no success (I'm beginning to think no one
knows how to do it...).

Could you please elaborate on how I would create the right macro to
generate the report, with as much detail as you are inclined to
provide?

I'm capable of doing it, I've just never studied microsoft products in
depth, before, and I just really want to pick someones brain for a few
minutes to get me started (I catch on fast...) Thanks!
 
M

Mdmax

"or you could create a macro that would read the data and prepare the
report."

Easy for you to say (haha), I honestly don't know how to get started, I
wish I had your experience.
 
G

Graham Mayor

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

My web site www.gmayor.com

<>>< ><<> ><<> <>>< ><<> <>>< <>><<>
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top