create and save separate files from mail merge result

G

gellione

i have a 108 page document containing 108 individualised copies of a letter, created by mail-merging Access data into a Word letter template. is there a way to break the long document into 108 separate documents and save them using data from the mail merge [name] fields as the filenames?

the long document contains page breaks before each new individualised letter so i guess it's possible to use a macro to creat a new document from each page break. but macros i've recorded with Word fail because they give each document the same filename. i don't know how to create macros outside of Word. and would appreciate any suggestions.

thank you.
 
G

gellione

thanks graham, that's a big help.

one non-essential tweaking question: is there a way to have the macro append the first few characters of text in each document to the resulting filenames?
 
G

Graham Mayor

The following will add the first word of each merge letter to the start of
the filenames. If that is derived from a field then that will reflect the
fieldname.
You can change the relevant bit of code to select the Word(s) you want to
include and of course change the path to match your own requirements

Sub SplitMerge()
' splitter Macro
' Macro created 16-08-98 by Doug Robbins to save each letter created by a
' mailmerge as a separate file.
' With minor modifications by Graham Mayor 10-02-03& 04-08-2004

Dim mask As String
Selection.EndKey Unit:=wdStory
Letters = Selection.Information(wdActiveEndSectionNumber)
mask = "ddMMyy"

Selection.HomeKey Unit:=wdStory
Counter = 1
While Counter < Letters
Application.ScreenUpdating = False

' Start of changed section
Selection.HomeKey Unit:=wdStory
Selection.MoveRight Unit:=wdWord, Count:=1, Extend:=wdExtend
sName = Selection
Docname = "D:\My Documents\Test\Merge\" _
& sName & " " & Format(Date, mask) & " " & LTrim$(Str$(Counter))
'End of changed section


ActiveDocument.Sections.First.Range.Cut
Documents.Add
Selection.Paste
ActiveDocument.SaveAs FileName:=Docname, _
FileFormat:=wdFormatDocument
ActiveWindow.Close
Counter = Counter + 1
Application.ScreenUpdating = True
Wend

End Sub

--
<>>< ><<> ><<> <>>< ><<> <>>< <>><<>
Graham Mayor - Word MVP


<>>< ><<> ><<> <>>< ><<> <>>< <>><<>
 
G

Graham Mayor

With a slight modification, you can delete the Word you used to name the
document, which might be handy if you inserted the extra field to provide
the name used -

Sub SplitbyDate()
' splitter Macro
' Macro created 16-08-98 by Doug Robbins to save each letter created by a
' mailmerge as a separate file.
' With minor modifications by Graham Mayor 10-02-03& 04-08-2004

Dim mask As String
Selection.EndKey Unit:=wdStory
Letters = Selection.Information(wdActiveEndSectionNumber)
mask = "ddMMyy"

Selection.HomeKey Unit:=wdStory
Counter = 1
While Counter < Letters
Application.ScreenUpdating = False
Selection.HomeKey Unit:=wdStory
Selection.MoveRight Unit:=wdWord, Count:=1, Extend:=wdExtend
sName = Selection
Selection.Delete Unit:=wdCharacter, Count:=1
Docname = "D:\My Documents\Test\Merge\" _
& sName & " " & Format(Date, mask) & " " & LTrim$(Str$(Counter))
ActiveDocument.Sections.First.Range.Cut
Documents.Add
Selection.Paste
ActiveDocument.SaveAs FileName:=Docname, _
FileFormat:=wdFormatDocument
ActiveWindow.Close
Counter = Counter + 1
Application.ScreenUpdating = True
Wend

End Sub


--
<>>< ><<> ><<> <>>< ><<> <>>< <>><<>
Graham Mayor - Word MVP


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

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