Consulting

Results 1 to 5 of 5

Thread: Mailmerge macros: File naming from header

  1. #1
    VBAX Newbie
    Joined
    Jan 2018
    Posts
    3
    Location

    Mailmerge macros: File naming from header

    I run the following macro to separate the mailmerged document into individual documents, the file names are as x iteration. So 1,2,3..
    I would like however to replace the filename and extract the header of the active file so each new file would have its individual header used as a file name. Ive tried replacing x as you may see below, to no avail. any ideas?

    I use the following code to separate from mail merged doc to individual docs. Works like a charm:

    Option Explicit

    Sub AllSectionsToSubDoc()

    Dim x As Long
    Dim Sections As Long
    Dim 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.ScreenUpdating = True
    Application.DisplayAlerts = True

    End Sub

    The next code is what Ive tried to extract the header;
    (basically the abive, replacing x in the save as line.)


    1. Option Explicit
    2. Sub AllSectionsToSubDoc()
    3. Dim x As Long
    4. Dim Sections As Long
    5. Dim Doc As Document
    6. Application.ScreenUpdating = False
    7. Application.DisplayAlerts = False
    8. Set Doc = ActiveDocument
    9. Sections = Doc.Sections.Count
    10. For x = Sections - 1 To 1 Step -1
    11. Doc.Sections(x).Range.Copy
    12. Documents.Add
    13. ActiveDocument.Range.Paste
    14. ActiveDocument.SaveAs (Doc.Path & "\" & ActiveWindow.ActivePane.View.SeekView = wdSeekCurrentPageHeader & ".doc")
    15. ActiveDocument.Close False
    16. Next x
    17. Application.ScreenUpdating = True
    18. Application.DisplayAlerts = True
    19. End Sub


    This didnt work -> syntax error in line 18, any ideas?

    Thanks in advance!

  2. #2
    Knowledge Base Approver VBAX Guru macropod's Avatar
    Joined
    Jul 2008
    Posts
    4,435
    Location
    It's not apparent what you mean by 'the header of the active file' in this case. It could be the mailmerge source header, a page header, or something else. That said, see:
    • Send Mailmerge Output to Individual Files;
    • Split Merged Output to Separate Documents
    in in the MailmergeTips and Tricks thread at:
    http://www.msofficeforums.com/mail-m...ps-tricks.html
    or:
    http://windowssecrets.com/forums/sho...ips-amp-Tricks


    Cheers
    Paul Edstein
    [Fmr MS MVP - Word]

  3. #3
    VBAX Newbie
    Joined
    Jan 2018
    Posts
    3
    Location
    Hi Paul,

    Thank you very much for your reply and help. Your posts have lots of great help.

    My issue is saving the merged document into individual documents with the output filename being the header of the document.

    Using the code above I have managed to separate the merged document into individual documents, which are saved however output filename 1, 2, 3.... Instead of x in the code above therefore I would like the output file ame of individual documents to contain the header of each document.

    I have looked at the links you have attached in the section;

    'Split Merged Output to Separate Documents' You have addressed my issue by mentioning how to change the desired output filename, however I still dont know how to use the header.

    For instance;
    As coded, it is assumed the output filename consists of the first paragraph in each record. If not, you could use a different range or replace all of the content between the ***** strings with code like
    Code:
    ' Construct the destination file path & name
    StrTxt = ActiveDocument.Path & Application.PathSeparator & (i + j - 1) / j


    OR ' Construct the destination file path & name
    StrTxt = ActiveDocument.Path & Application.PathSeparator & StrTxt

    Instead I would like to add the Header

    Any tips for this?

    Many Thanks!

    Z

  4. #4
    Knowledge Base Approver VBAX Guru macropod's Avatar
    Joined
    Jul 2008
    Posts
    4,435
    Location
    All you may need to do is change:
    Set Rng = .Range.Paragraphs(1).Range
    to, say:
    Set Rng = .Headers(wdHeaderFooterPrimary).Range.Paragraphs(1).Range
    Cheers
    Paul Edstein
    [Fmr MS MVP - Word]

  5. #5
    VBAX Newbie
    Joined
    Jan 2018
    Posts
    3
    Location
    Cheers Paul.

    That's all it needed. Issue solved.

    Thanks,
    Z

Tags for this Thread

Posting Permissions

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