Please Note: This article is written for users of the following Microsoft Word versions: 97, 2000, 2002, and 2003. If you are using a later version (Word 2007 or later), this tip may not work for you. For a version of this tip written specifically for later versions of Word, click here: Merging to Individual Files.

Merging to Individual Files

by Allen Wyatt
(last updated April 21, 2017)

39

When you use the mail-merge feature in Word, you have the option of either printing your results directly or creating a new document. If you choose the latter option, the resulting document contains all the information that normally would have gone to the printer. For instance, if your merge document is a two-page letter, each letter in the resulting document is separated by a section break. Thus, if you are merging the two-page letter with 150 different addresses, then you would end up with a 300-page document with 150 section breaks (one every two pages).

For some people, it would be more helpful if you could merge to individual files. For instance, given the above example you might want 150 individual document files, each two pages long. Unfortunately, that is not one of the options available when using Word's mail-merge feature. The only answer to accomplishing this task is to create a macro that will, based on the merged document (the 300-page one mentioned above) take each section and save it to its own document file.

If you look in Microsoft's Knowledge Base, you will find that there is an old article that deals with this very issue:

http://support.microsoft.com/kb/216201

The macro presented in the article, even though it was written for an older version of Word, will work just fine in later versions. It allows you to separate a merged document into individual files, based on the section breaks:

Sub BreakOnSection()
    'Used to set criteria for moving through the document by section.
    Application.Browser.Target = wdBrowseSection

    'A mailmerge document ends with a section break next page.
    'Subtracting one from the section count stop error message.
    For i = 1 To ((ActiveDocument.Sections.Count) - 1)

        'Select and copy the section text to the clipboard
        ActiveDocument.Bookmarks("\Section").Range.Copy

        'Create a new document to paste text from clipboard.
        Documents.Add
        Selection.Paste

        'Removes the break that is copied at the end of the section, if any.
        Selection.MoveUp Unit:=wdLine, Count:=1, Extend:=wdExtend
        Selection.Delete Unit:=wdCharacter, Count:=1

        ChangeFileOpenDirectory "C:\"
        DocNum = DocNum + 1
        ActiveDocument.SaveAs FileName:="test_" & DocNum & ".doc"
        ActiveDocument.Close
        'Move the selection to the next section in the document
        Application.Browser.Next
    Next i
    ActiveDocument.Close savechanges:=wdDoNotSaveChanges
End Sub

It is important to realize, however, that this macro will only work if the only place that section breaks occur is between merged documents. If your original document includes section breaks, then the macro will not provide satisfactory results. For additional information on using the macro, refer to the Knowledge Base article at the link provided above.

WordTips is your source for cost-effective Microsoft Word training. (Microsoft Word is the most popular word processing software in the world.) This tip (1538) applies to Microsoft Word 97, 2000, 2002, and 2003. You can find a version of this tip for the ribbon interface of Word (Word 2007 and later) here: Merging to Individual Files.

Author Bio

Allen Wyatt

With more than 50 non-fiction books and numerous magazine articles to his credit, Allen Wyatt is an internationally recognized author. He  is president of Sharon Parq Associates, a computer and publishing services company. ...

MORE FROM ALLEN

Adding Tabs at the Beginning of a Line

Press a tab at the beginning of a paragraph, and Word normally assumes you want to indent the paragraph. If you don't like ...

Discover More

Calculating Average Word Length

Word keeps track of many statistics about your documents. The problem, it seems, is getting exactly the statistics you need ...

Discover More

Creating Charts in VBA

Most charts you create in Excel are based on information stored in a worksheet. You can also create charts based on ...

Discover More

The First and Last Word on Word! Bestselling For Dummies author Dan Gookin puts his usual fun and friendly candor back to work to show you how to navigate Word 2013. Spend more time working and less time trying to figure it all out! Check out Word 2013 For Dummies today!

More WordTips (menu)

Displaying Path Names in the Menu Bar

Want the full path name for a document visible on the screen? Easily add it to a menu bar.

Discover More

Removing All File Properties

Want to get rid of any properties you've created for a document? You can do so by using the short macro described in this ...

Discover More

Positioning the Cursor in a New Document

Creating special templates is a great way to establish "standards" for your documents. With a little ingenuity you can even ...

Discover More
Subscribe

FREE SERVICE: Get tips like this every week in WordTips, a free productivity newsletter. Enter your address and click "Subscribe."

View most recent newsletter.

Comments

If you would like to add an image to your comment (not an avatar, but an image to help in making the point of your comment), include the characters [{fig}] in your comment text. You’ll be prompted to upload your image when you submit the comment. Maximum image size is 6Mpixels. Images larger than 600px wide or 1000px tall will be reduced. Up to three images may be included in a comment. All images are subject to review. Commenting privileges may be curtailed if inappropriate images are posted.

What is 4 - 3?

2017-03-08 16:19:31

Jim

Hi, I tried sending multiple documents via an email and word 2016 has merged them.
Trouble is that anything I do in word now gets merged into that file and I've no clue what's happened or how to undo it.

Any help from anyone is greatly appreciated
Thanks


2017-02-28 19:29:44

SIM

Hi I have the same question as many peoples here
I would like to know how to save a document with a name.

Because if we have 33 documents by example title Name_1, Name _2 etc.. it's hard to know witch letter to find. when they are all saved

I would like to used either a merge field ex ``name`` or a particular line in the document but not the first one as the first line taken for the logo.

Please help : The goal was to used a spreedsheet in excel and fill out individual letter, I achieved that but I have an issue with the naming of each individual document to generic if I used the template from this article.

Also wondering if someone know why using allen macro the spacing goes from 1 to 1.5. I can live with that, but if someone know why that would be great. thank you again to all!


2016-07-04 01:00:11

jc

Hi,

I would like to know what to edit on David's macro so that the name of each file is saved using

1. the second line of the form

OR

2. a merge field <<First Name>><<Last Name>>


2016-02-04 17:04:15

Dale

Hi

I'm having the same issue as in Claire's post from 13 Jan 2016. Does anyone know why the data does not pull through onto the last page (I have a 4 page document, it creates a 4 page document but only the first 3 pages have the data on them).

Dale


2016-01-13 06:41:22

Claire

Hello

I can get the macro to work but it stops before the last page. It stops on page 2 and there are 3 pages - does the number of pages make a difference? I have double checked the document for Section Breaks using ctrl+F and couldn't find any.

Claire


2015-12-17 10:31:54

Missy

What do you do with this script? Where do you put it so it will work. Everytime I look up directions it says nothing about scripts.


2015-11-26 11:16:17

Alvin Scott

If you are happy to have the document saved as a PDF you can do this to save individual letters from Word.
In Word 2007 at least - and probably in any version since then - you can save individual mail merge letters as PDFs this way:
With the merge open and ready to save:
• Click the office button and go to Save As (don't try to use the Finish and Merge button as this will save the whole merge).
• Select the destination where you want to save the file and name the file
• Change the file type to PDF then click on the Options button
• If the document is a single page, select current page or if it has more pages, select the page range required
• Click OK then OK again
• Adobe Reader will open its preview of how it will look
• Close the preview to return to your merge and continue from there to the next recipient
Hope this helps


2015-11-12 16:53:29

Bobbi

This was so great! It didn't work at first, then I realized I had to change the location (ChangeFileOpenDirectory "C:") to save to a jump drive (E) and then it worked great.


2015-11-04 22:20:25

elizabeth

I got this script to work and its great but when I split up my document the copies don't keep the original formatting of the word file. The fonts are wrong and some of the tables are moved over.

I can't put addon on my PC is there a macro I can do so my original document is used as a template


2015-07-12 19:16:53

Yolanda Spencer

Hi

I am new to VBE, and also am running on a MacBook Pro
When I run this macro (thanks so much for it) it is showing an error 'method or data not found' re .FileDialog

Any help really appreciated, Thanks


2015-06-21 09:51:44

Rebecca

Hi, I keep getting a syntax error when trying to save the file. Any suggestions? Thanks,


2015-05-23 00:58:53

Keith

Loren
My macro also adds an extra blank page when saving separate Word mail merge files. Any suggestions?


2015-05-06 11:37:10

Liam B

Hi Guys,

Thanks the macro works great for selecting a folder and saving each individual letter to this location.

However i would like to save each file with a unique ID specific to each individual file (i.e. the name of the person the letter is for).

Can I pull back a field from my merged letter and use this field to form part of the name the file will be saved as.

such as below

' ActiveDocument.SaveAs FileName:="NameVariable" & DocNum & ".doc"

Thank you kindly,
Liam


2015-05-06 11:35:20

Liam

Hi Guys,

Thanks the macro works great for selecting a folder and saving each individual letter to this location.

However i would like to save each file with a unique ID specific to each individual file (i.e. the name of the person the letter is for).

Can I pull back a field from my merged letter and use this field to form part of the name the file will be saved as.

such as below

' ActiveDocument.SaveAs FileName:="NameVariable" & DocNum & ".doc"

Thank you kindly,
Liam


2015-05-05 22:05:52

Sara

Roxanne,

Thank you so much for providing the edited macro - it's fabulous! I'm only having one issue: it appears to repeat the first page, saving it again as a second document. Any fix?

Thanks!
-Sara


2015-04-28 06:00:21

James

Eric see http://www.slipstick.com/developer/code-samples/paste-clipboard-contents-vba/ for more detailed instructions on how to reference the forms library.


2015-04-28 05:57:59

James

Hi Eric, it's because you don't have the Microsoft Forms 2.0 Object Library. I wrote before:

"You'll need the Microsoft Forms 2.0 Object Library for this: "If you receive a "User-defined type not defined" you are missing the reference to Microsoft Forms 2.0 Object Library. If its not listed, add C:WindowsSystem32FM20.dll or C:WindowsFM20.dll as a reference". References can be found in the VBA editor under Tools. "


2015-04-27 20:46:38

Eric

I ran the macro from James 05 mar 2015 in Office 2013 and I got a "User-defined type not defined" on line 192
'Stores the first line as an object to use in the save file
Dim DataObj As New MSForms.DataObject

Any ideas on how to fix this would be much appreciated.


2015-04-07 21:18:55

Roxxanne

This is the macro I created. It the original but you can choose where to save it, it changes the margins on the page it's pasted on and then it removes the section break after it has pasted.

By removing the section break it means that there should be no blank page at the end.

You'll have to replace the margins section with the margins you want or delete the section

Sub BreakOnSection()
' Select a folder, change the default file save location below so it's not just C:
Dim strFolder As String
Set fd = Application.FileDialog(msoFileDialogFolderPicker)
With fd
.Title = "Select the folder into which the documents will be saved."
If .Show = -1 Then
strFolder = .SelectedItems(1) & ""
Else
MsgBox "The documents will be saved in the default document file location."
strFolder = "C:"
End If
End With
ChangeFileOpenDirectory strFolder

'Used to set criteria for moving through the document by section.
Application.Browser.Target = wdBrowseSection

'A mailmerge document ends with a section break next page.
'Subtracting one from the section count stop error message.
For i = 1 To ((ActiveDocument.Sections.Count) - 1)

'Select and copy the section text to the clipboard
ActiveDocument.Bookmarks("Section").Range.Copy

'Create a new document to paste text from clipboard.
Documents.Add
Selection.PasteAndFormat (wdFormatOriginalFormatting)

'Removes the break that is copied at the end of the section, if any.
Selection.MoveUp Unit:=wdLine, Count:=1, Extend:=wdExtend
Selection.Delete Unit:=wdCharacter, Count:=1

'Changes margins'
With ActiveDocument.PageSetup
.TopMargin = CentimetersToPoints(1.27) '1.27cm
.BottomMargin = CentimetersToPoints(1.27)
.LeftMargin = CentimetersToPoints(1.27) '1.27
.RightMargin = CentimetersToPoints(1.27)
End With

'Replaces section breaks with space'
Selection.Find.ClearFormatting
Selection.Find.Replacement.ClearFormatting
With Selection.Find
.Text = "^b"
.Replacement.Text = " "
.Forward = True
.Wrap = wdFindContinue
.Format = False
.MatchCase = False
.MatchWholeWord = False
.MatchWildcards = False
.MatchSoundsLike = False
.MatchAllWordForms = False
End With
Selection.Find.Execute Replace:=wdReplaceAll

DocNum = DocNum + 1
' ActiveDocument.SaveAs FileName:="test_" & DocNum & ".doc"
ActiveDocument.SaveAs
ActiveDocument.Close
'Move the selection to the next section in the document
Application.Browser.Next
Next i
ActiveDocument.Close savechanges:=wdDoNotSaveChanges
End Sub

Sub Demo()
With ActiveDocument.Content.Find
.ClearFormatting
.Replacement.ClearFormatting
.Text = "^12[^12^13 ]{1,}"
.Replacement.Text = "^12"
.Forward = True
.Wrap = wdFindContinue
.Format = False
.MatchWildcards = True
.Execute Replace:=wdReplaceAll
End With
End Sub


2015-04-06 20:59:27

Roxxanne

Hi,

I'm using this and I want to save as a Word doc, not a pdf, but when the document saves there's a blank page afterwards.

Any ideas?


2015-03-05 01:26:54

James

Hi Jon C. I changed the code a tiny bit so that when it saves as a PDF it saves the whole document, that was a mistake on my part. Note, there should only be 1 section break between mail merges for this to work.

Slightly edited code (parts of this are redundant but it should work regardless):

Sub Save_Merged_As_PDFs2()

'Makes the code run faster and reduces screen flicker a bit.
Application.ScreenUpdating = False

' Select a folder, change the default file save location below so it's not just C:
Dim strFolder As String
Set fd = Application.FileDialog(msoFileDialogFolderPicker)
With fd
.Title = "Select the folder into which the documents will be saved."
If .Show = -1 Then
strFolder = .SelectedItems(1) & ""
Else
MsgBox "The documents will be saved in the default document file location."
strFolder = "C:"
End If
End With
ChangeFileOpenDirectory strFolder

'Used to set criteria for moving through the document by section.
Application.Browser.Target = wdBrowseSection

'A mailmerge document ends with a section break next page.
'Subtracting one from the section count stop error message.
For i = 1 To ((ActiveDocument.Sections.Count) - 1)

'Select and copy the section text to the clipboard
ActiveDocument.Bookmarks("Section").Range.Copy

'Create a new document to paste text from clipboard.
Documents.Add
Selection.PasteAndFormat (wdFormatOriginalFormatting)

'Removes the break that is copied at the end of the section, if any.
Selection.MoveUp Unit:=wdLine, Count:=1, Extend:=wdExtend
Selection.Delete Unit:=wdCharacter, Count:=1

'Collects the first line of the document to use in the save file
Selection.MoveUp Unit:=wdScreen, Count:=5
Selection.MoveDown Unit:=wdLine, Count:=1, Extend:=wdExtend
Selection.Copy
'Deletes the first line so it doesn't show up in the PDF
Selection.Delete


'Stores the first line as an object to use in the save file
Dim DataObj As New MSForms.DataObject
DataObj.GetFromClipboard
Dim my_filename As String
my_filename = DataObj.GetText


DocNum = DocNum + 1
'Exports the Document as a PDF, with file name as the stored object
ActiveDocument.ExportAsFixedFormat OutputFileName:= _
"C:" & my_filename & " Coversheet.pdf", _
ExportFormat:=wdExportFormatPDF, OpenAfterExport:=False, OptimizeFor:= _
wdExportOptimizeForPrint, Range:=wdExportAllDocument, From:=1, To:=1, _
Item:=wdExportDocumentContent, IncludeDocProps:=True, KeepIRM:=True, _
CreateBookmarks:=wdExportCreateNoBookmarks, DocStructureTags:=True, _
BitmapMissingFonts:=True, UseISO19005_1:=False
ActiveDocument.Close savechanges:=wdDoNotSaveChanges
'Move the selection to the next section in the document
Application.Browser.Next
Next i
ActiveDocument.Close savechanges:=wdDoNotSaveChanges
End Sub

Good luck!


2015-03-02 09:19:00

Jon C

I have used the variation added by James 24 Feb and it works brilliantly, except if the original 'section' for a given merge runs over more than one page I get an error. Can anyone help me resolve this?


2015-02-24 00:37:09

James

I made some changes to David's macro so that:

1. It saves as a PDF instead of a word doc
2. The filename is taken from the first line of the document (and then the line is deleted to not show up in the pdf doc)

Write in the first line of your mail merge what you want each file to be called, e.g. <<Header1>> Form 1

This was my first VBA macro edit so it may be messy but hopefully it works for you too!


Sub Save_Merged_As_PDFs()

' Select a folder, change the default file save location below so it's not just C:
Dim strFolder As String
Set fd = Application.FileDialog(msoFileDialogFolderPicker)
With fd
.Title = "Select the folder into which the documents will be saved."
If .Show = -1 Then
strFolder = .SelectedItems(1) & ""
Else
MsgBox "The documents will be saved in the default document file location."
strFolder = "C:"
End If
End With
ChangeFileOpenDirectory strFolder

'Used to set criteria for moving through the document by section.
Application.Browser.Target = wdBrowseSection

'A mailmerge document ends with a section break next page.
'Subtracting one from the section count stop error message.
For i = 1 To ((ActiveDocument.Sections.Count) - 1)

'Select and copy the section text to the clipboard
ActiveDocument.Bookmarks("Section").Range.Copy

'Create a new document to paste text from clipboard.
Documents.Add
Selection.PasteAndFormat (wdFormatOriginalFormatting)

'Removes the break that is copied at the end of the section, if any.
Selection.MoveUp Unit:=wdLine, Count:=1, Extend:=wdExtend
Selection.Delete Unit:=wdCharacter, Count:=1

'Collects the first line of the document to use in the save file
Selection.MoveUp Unit:=wdScreen, Count:=5
Selection.MoveDown Unit:=wdLine, Count:=1, Extend:=wdExtend
Selection.Copy
'Deletes the first line so it doesn't show up in the PDF
Selection.Delete


'Stores the first line as an object to use in the save file
Dim DataObj As New MSForms.DataObject
DataObj.GetFromClipboard
Dim my_filename As String
my_filename = DataObj.GetText


DocNum = DocNum + 1
'Exports the Document as a PDF, with file name as the stored object, remember to change the output file name path to the same as your default from above, do this in the OutputFileName below
ActiveDocument.ExportAsFixedFormat OutputFileName:= _
"C:" & my_filename & " Coversheet.pdf", _
ExportFormat:=wdExportFormatPDF, OpenAfterExport:=False, OptimizeFor:= _
wdExportOptimizeForPrint, Range:=wdExportCurrentPage, From:=1, To:=1, _
Item:=wdExportDocumentContent, IncludeDocProps:=True, KeepIRM:=True, _
CreateBookmarks:=wdExportCreateNoBookmarks, DocStructureTags:=True, _
BitmapMissingFonts:=True, UseISO19005_1:=False
ActiveDocument.Close savechanges:=wdDoNotSaveChanges
'Move the selection to the next section in the document
Application.Browser.Next
Next i
ActiveDocument.Close savechanges:=wdDoNotSaveChanges
End Sub

You'll need the Microsoft Forms 2.0 Object Library for this: "If you receive a "User-defined type not defined" you are missing the reference to Microsoft Forms 2.0 Object Library. If its not listed, add C:WindowsSystem32FM20.dll or C:WindowsFM20.dll as a reference". References can be found in the VBA editor under Tools.

Good luck everyone!


2015-02-17 00:27:01

Jessica

Hi there,

How would I update this Macro so that the file name would be the second or third line of text in the document (not this first as per David Smith's macro).

Thank you!


2015-02-04 09:45:21

Georgia Robinsky

I never did a macro before -
Is there any instructions on how to write your macro into what i need to do?
Thanks


2015-01-22 12:32:41

Angela

THIS WAS THE BEST TIP EVER!! Ok, a little over the top but you get the meaning! Thanks.


2015-01-16 12:29:37

Anne

Thanks! Saved me for a lot of work!


2015-01-14 13:32:49

Joyce

This is SO helpful, thanks so much. It worked for me on Word for Mac 2011 by the way. It took me a while to figure out how to do "PasteandFormat" -- I should have read the comments first! Thanks to David Smith for the suggestion and code for naming by the first line of the form.


2015-01-11 15:26:55

Grateful

Dude - You are AWESOME! Thanks!!


2014-12-12 16:24:13

David Smith

Here is my code. I added a folder picker at the beginning, did PasteAndFormat and removed the hard coded name (so it takes the name of each file from the first line of the form.

Sub BreakOnSection()
' Select a folder
Dim strFolder As String
Set fd = Application.FileDialog(msoFileDialogFolderPicker)
With fd
.Title = "Select the folder into which the documents will be saved."
If .Show = -1 Then
strFolder = .SelectedItems(1) & ""
Else
MsgBox "The documents will be saved in the default document file location."
strFolder = "c:"
End If
End With
ChangeFileOpenDirectory strFolder

'Used to set criteria for moving through the document by section.
Application.Browser.Target = wdBrowseSection

'A mailmerge document ends with a section break next page.
'Subtracting one from the section count stop error message.
For i = 1 To ((ActiveDocument.Sections.Count) - 1)

'Select and copy the section text to the clipboard
ActiveDocument.Bookmarks("Section").Range.Copy

'Create a new document to paste text from clipboard.
Documents.Add
Selection.PasteAndFormat (wdFormatOriginalFormatting)

'Removes the break that is copied at the end of the section, if any.
Selection.MoveUp Unit:=wdLine, Count:=1, Extend:=wdExtend
Selection.Delete Unit:=wdCharacter, Count:=1

DocNum = DocNum + 1
' ActiveDocument.SaveAs FileName:="test_" & DocNum & ".doc"
ActiveDocument.SaveAs
ActiveDocument.Close
'Move the selection to the next section in the document
Application.Browser.Next
Next i
ActiveDocument.Close savechanges:=wdDoNotSaveChanges
End Sub


2014-11-28 07:25:14

ali

This has saved my bacon today -thank you for posting. Did as others suggested and changed the file location.


2014-11-25 18:03:44

Joni

When I use this, it changes the format of my letter (eg: it changes the font size, font etc.). How do I keep it so the font is the same as my original document so I won't have to format everything?


2014-11-11 14:26:09

Mir

Can anybody walk me step by step on how to create and run this macro? I have tried different things, but when I hit "Run" my word document closes and I get no results. This is the first time I try macros. So, I'm a complete newbie. Thanks.


2014-07-23 16:00:03

Julian Mackenzie

I ran the macro a first time and it worked very well indeed. On my second attempt it would only save the first page of a 2 page document and I cannot work out why. Any suggestions would be welcome.
Thanks


2014-06-08 08:55:49

F. Zohra

Very useful tips.
To do mail merge I use data saved in excel,is there a way to force the code to save the file using one of excel field. e.g. save as order # (order # is not in series)which is in my excel column after merging the files and spliting it. please help


2014-05-20 10:00:02

Barjesh Syal

Very nice code... Its great...


2014-05-19 13:16:40

Loren

OK, I figured out how to paste it with original formatting: instead of Selection.Paste, use Selection.PasteAndFormat(wdFormatOriginalFormatting).

But it is still pasting the section break, or something, so each document has an extra empty page at the end.


2014-05-19 13:06:30

Loren

Thanks. This works great, except that VBA ignores my line spacing when it pastes, so the formatting is messed up. When cutting and pasting using the mouse, the spacing is not messed up. Do you know a simple way of modifying this code so that it will preserve things like line spacing? Thanks again.


2014-04-30 13:06:32

Lisa Hochhauser

Thanks for this macro. Saved me a lot of time!


Two suggested upgrades to this script would be to

1. Let you save it in the location you started from rather than the C drive.

2. Use the first few words of the file to name it (ala word's saving method)


This Site

Got a version of Word that uses the menu interface (Word 97, Word 2000, Word 2002, or Word 2003)? This site is for you! If you use a later version of Word, visit our WordTips site focusing on the ribbon interface.

Newest Tips
Subscribe

FREE SERVICE: Get tips like this every week in WordTips, a free productivity newsletter. Enter your address and click "Subscribe."

(Your e-mail address is not shared with anyone, ever.)

View the most recent newsletter.