PDA

View Full Version : Setting document names after separating mail merge files into individual documents.



chemprac
08-17-2014, 12:53 PM
Hi,

So I am using this code to separate mail merged files into separate documents but I am having trouble defining a variable that would allow me to choose the names for these files.

I use this module to separate the files into individual ones, then they are named, 1.doc, 2.doc etc. Instead I want them to be named as the title of each document (Characters that are on the first line.)

Option ExplicitSub
AllSectionsToSubDoc()
Dim x AsLongDim
Sections AsLongDim
Doc As Document
Application.ScreenUpdating = False
Application.DisplayAlerts = False
Set Doc = ActiveDocument
Sections = Doc.Sections.Count
For x = Sections - 1 To 1 Step -1 Doc.
Sections(x).Range.Copy Documents.Add
ActiveDocument.Range.Paste
ActiveDocument.SaveAs (Doc.Path & "\" & x & ".doc")
ActiveDocument.Close False
Next x Application.Screen
Updating = True
Application.DisplayAlerts = True
End Sub

So for example, the documents are like

Sydney Airport

BLAG AHBLKJ
ADKJHSDF


and then

Melbourne Airport

SDJHFJS
AJHKSDJFH

etc.

I want the documents to be called Sydney Airport and Melbourne Airport etc.. Can someone help me modify the code to do this?

Thanks and let me know if I did anything wrong, I am new to the forum.

macropod
08-17-2014, 03:51 PM
See the macro for the 'Split Merged Output to Separate Documents' topic at: http://www.msofficeforums.com/mail-merge/21803-mailmerge-tips-tricks.html. IMHO, though, you'd probably do better to automate the mailmerge, using the macro from the 'Send Mailmerge Output to Individual Files' topic instead.

chemprac
08-18-2014, 05:18 AM
Thank you,

with a few modifications, exactly what I needed.

gmayor
08-18-2014, 10:07 PM
There is plenty of scope when splitting a merged document in this manner for (e.g) filenames to be duplicated and for illegal filenames to screw things up. I produced a simple (as far as the user is concerned) process to overcome the obvious issues and always create unique names. Take a look at my web site (in the signature) for Merge and Split.

macropod
08-18-2014, 10:50 PM
There is plenty of scope when splitting a merged document in this manner for (e.g) filenames to be duplicated and for illegal filenames to screw things up.
Indeed there is. The 'Send Mailmerge Output to Individual Files' example was coded for that and I've now added the same logic to the 'Split Merged Output to Separate Documents' example.

chemprac
08-19-2014, 12:48 AM
There was no problem with the code. I had to modify it because windows 8 does not allow saving right to the C Drive, so had to add it a different folder as filename. But apart from that the code worked beautifully.