If your data source is an Access Database, starting with a mailmerge main
document to which the datasource is attached and which contains formfields,
the following macro will produce individual documents for each record in the
datasource with each of those documents containing the formfields so that
they would be available for your users to complete their part of the data.
To make use of the macro, it is necessary to set a reference in the Visual
Basic Editor (Tools>References) to the Microsoft DAO 3.6 Object Library.
Sub MailMergewithFormFields()
Dim dSource As String
Dim qryStr As String
Dim mfCode As Range
Dim i As Long, j As Long
Dim db As DAO.Database
Dim rs As DAO.Recordset
With ActiveDocument
'Get the details of the datasource
With .MailMerge.DataSource
dSource = .Name
qryStr = .QueryString
End With
'Convert the MERGEFIELDS to DOCVARIABLE fields
For i = 1 To .Fields.Count
If .Fields(i).Type = wdFieldMergeField Then
Set mfCode = .Fields(i).code
mfCode = Replace(mfCode, "MERGEFIELD", "DOCVARIABLE")
End If
Next i
'Convert the Mail Merge Main document to a normal Word document
.MailMerge.MainDocumentType = wdNotAMergeDocument
End With
' Open the database
Set db = OpenDatabase(dSource)
' Retrieve the recordset
Set rs = db.OpenRecordset(qryStr)
With rs
' Move to the first record
.MoveFirst
j = 1
Do While Not .EOF
'Create variables in the document with the names and values of the
fields in each record
For i = 0 To .Fields.Count - 1
If .Fields(i).Value <> "" Then
ActiveDocument.Variables(.Fields(i).Name).Value =
..Fields(i).Value
End If
Next i
ActiveDocument.Fields.Update
ActiveDocument.SaveAs "C:\Test\MwithFF" & j
.MoveNext
j = j + 1
Loop
End With
rs.Close
db.Close
Set rs = Nothing
Set db = Nothing
End Sub
Here is another macro that executes the merge to a new document in which it
will reinstate the formfields to the relevant places in that document and
then split that document into individual documents, which it then protects
for filling in formfields.. It only works with TextInput type FormFields
however
Sub MergewithFormFields()
Dim i As Long
Dim Source As Document, Target As Document
Dim Letter As Range
With ActiveDocument
For i = .FormFields.Count To 1 Step -1
If .FormFields(i).Type = wdFieldFormTextInput Then
.FormFields(i).Range.Text = "FF" & i
End If
Next i
With .MailMerge
.Destination = wdSendToNewDocument
.Execute
End With
End With
Selection.HomeKey wdStory
With Selection.Find
Do While .Execute(FindText:="FF[0-9]{1,}", Forward:=True, _
MatchWildcards:=True, Wrap:=wdFindStop, MatchCase:=True) = True
ActiveDocument.FormFields.Add Selection.Range, wdFieldFormTextInput
Loop
End With
Set Source = ActiveDocument
For i = 1 To Source.Sections.Count
Set Letter = Source.Sections(i).Range
Set Target = Documents.Add
Target.Range = Letter
Target.Sections(2).PageSetup.SectionStart = wdSectionContinuous
Target.Protect wdAllowOnlyFormFields
Target.SaveAs FileName:="Letter" & i
Target.Close
Next i
End Sub
--
Hope this helps.
Please reply to the newsgroup unless you wish to avail yourself of my
services on a paid consulting basis.
Doug Robbins - Word MVP, originally posted via msnews.microsoft.com
My users are located across multiple cities and the pre-populated
documents will be emailed to their office. I will be pulling reports
to complete the forms with general information like names and
addresses so the users will not have access to the data.
We are trying to make it easier for our people in the field to
complete their paperwork. But there are a few fields that they must
fill out themselves.
Thanks!
Christine