Hi, im trying to separate mail merge documents into separate files so i can save each letter in a mail merge as a separate file.
that is what i've been given as a guide - yet it makes no real sense and although i've followed it as much as i can, it isnt giving me anything useful.
any help at all would be really appreciated as i need to be able to do this asap.
thanks
Jay
Code:
Save each merged letter as a separate file
Normally, the mail merge result to a new document is one long file which can be edited and printed. Using the Master Document feature, it's possible to save each as a separate file. In the mail merge result document, each record's letter is created in its own section, so one can select each section, turn it into a sub-document, open the sub-document and save it as a separate file.
The only preparation you need to make in the main merge document is to select the first paragraph, go to Format/Paragraph and set the Outline Level to 1 (one). The Master Document feature needs this in order to create the sub-document.
The following sample VBA code can help automate this process.
Sub SaveRecsAsFiles
' Convert all sections to Subdocs
AllSectionsToSubDoc ActiveDocument
'Save each Subdoc as a separate file
SaveAllSubDocs ActiveDocument
End Sub
Sub AllSectionsToSubDoc(ByRef doc As Word.Document)
Dim secCounter As Long
Dim NrSecs As Long
NrSecs = doc.Sections.Count
'Start from the end because creating
'Subdocs inserts additional sections
For secCounter = NrSecs - 1 To 1 Step -1
doc.Subdocuments.AddFromRange _
doc.Sections(secCounter).Range
Next secCounter
End Sub
Sub SaveAllSubDocs(ByRef doc As Word.Document)
Dim subdoc As Word.Subdocument
Dim newdoc As Word.Document
Dim docCounter As Long
docCounter = 1
'Must be in MasterView to work with
'Subdocs as separate files
doc.ActiveWindow.View = wdMasterView
For Each subdoc In doc.Subdocuments
Set newdoc = subdoc.Open
'Remove NextPage section breaks
'originating from mailmerge
RemoveAllSectionBreaks newdoc
With newdoc
.SaveAs FileName:="MergeResult" & CStr(docCounter)
.Close
End With
docCounter = docCounter + 1
Next subdoc
End Sub
Sub RemoveAllSectionBreaks(doc As Word.Document)
With doc.Range.Find
.ClearFormatting
.Text = "^b"
With .Replacement
.ClearFormatting
.Text = ""
End With
.Execute Replace:=wdReplaceAll
End With
End Sub
that is what i've been given as a guide - yet it makes no real sense and although i've followed it as much as i can, it isnt giving me anything useful.
any help at all would be really appreciated as i need to be able to do this asap.
thanks
Jay