Consulting

Results 1 to 6 of 6

Thread: Setting document names after separating mail merge files into individual documents.

  1. #1
    VBAX Newbie
    Joined
    Aug 2014
    Posts
    3
    Location

    Setting document names after separating mail merge files into individual documents.

    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.

  2. #2
    Knowledge Base Approver VBAX Guru macropod's Avatar
    Joined
    Jul 2008
    Posts
    4,435
    Location
    See the macro for the 'Split Merged Output to Separate Documents' topic at: http://www.msofficeforums.com/mail-m...ps-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.
    Cheers
    Paul Edstein
    [Fmr MS MVP - Word]

  3. #3
    VBAX Newbie
    Joined
    Aug 2014
    Posts
    3
    Location
    Thank you,

    with a few modifications, exactly what I needed.

  4. #4
    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.
    Graham Mayor - MS MVP (Word) 2002-2019
    Visit my web site for more programming tips and ready made processes
    http://www.gmayor.com

  5. #5
    Knowledge Base Approver VBAX Guru macropod's Avatar
    Joined
    Jul 2008
    Posts
    4,435
    Location
    Quote Originally Posted by gmayor View Post
    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.
    Cheers
    Paul Edstein
    [Fmr MS MVP - Word]

  6. #6
    VBAX Newbie
    Joined
    Aug 2014
    Posts
    3
    Location
    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.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •