#  Other Applications & Softwares  > Word Formatting & General >  >  Splitting and renaming mailmerge doc - new to macros!

## Debbieannbee

:Confused: Hi there - I am currently running mail merges containing hundreds of separate letters.  I am using the following macro to split the doc:

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

   'A mail merge 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)

'Note: If a document does not end with a section break,
'substitute the following line of code for the one above:
'For I = 1 To ActiveDocument.Sections.Count

      '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

I would like to know if it's possible to add to this macro in order for it to split the doc and the rename the new doc using the info on the top line of each new doc.  This is already in the new mailmerge.  I hope I am explaining myself properly!

----------


## macropod

Yes, that's possible, but you're going about the process the wrong way around, IMHO. Ideally, you'd use a macro to drive the merge process and save each record's file as it goes. For example:



```
Please Login or Register  to view this content.
```


The above macro generates one output file per record. As coded, files are saved to the same folder as the mailmerge main document, using the Last_Name & First_Name fileds in the data source for the filenames. PDF & DOCX formats are catered for.

PS: When posting code, please use the code tags. They're on the 'Go Advanced' tab.

----------


## Debbieannbee

Thanks for the reply Paul - the problem I have is that I am receiving the mailmerge as a "done deal" from an external source and having to split the doc thereafter  :Frown:

----------


## macropod

Assuming your naming data consists of the entire first paragraph, try the following:



```
Please Login or Register  to view this content.
```

----------


## Debbieannbee

Fantastic!  Works perfectly, appreciated!  :Smilie: 

Debbie  :Smilie:

----------


## Debbieannbee

Having completely appreciated all your help so far!  Would you know if it's possible to do the same as above, with a set of documents containing two section breaks?  (Essentially documents with a front cover).  Obviously this splits at the first second break, and I will need it to split at the second.  Sorry to be a nuisance - my lack of macro understanding is not helpful!

----------


## macropod

If your front cover had been created via a 'Different First Page' setup, the same code could be used. That said, the following version of the macro should work with any consistent number of Sections per record:



```
Please Login or Register  to view this content.
```

----------


## Debbieannbee

Thanks again macropod - you've saved me loads of time  :Smilie:

----------

