none
Save merge results to individual files

    Question

  • Ref my earlier question:

    http://social.msdn.microsoft.com/Forums/en-US/worddev/thread/8d232bf2-3325-4049-897d-7da30b8e26e6

    Thanks to Doug for his help..

    Private Sub Document_Open()
    Dim fldrname As String
    With ActiveDocument.MailMerge
        Destination = wdSendToNewDocument
        MailAsAttachment = False
        MailAddressFieldName = ""
        MailSubject = ""
        SuppressBlankLines = True
        With .DataSource
            .ActiveRecord = wdFirstRecord
            fldrname = .DataFields("FolderNo").Value
            FirstRecord = wdDefaultFirstRecord
            LastRecord = wdDefaultLastRecord
        End With
        Execute Pause:=True
    End With
    ActiveDocument.SaveAs FileName:="C:\My DataBase\WordQuotes\" & fldrname & "\Thank You Letter.doc"
    End Sub

    The line starting..'ActiveDocument.SaveAs' works great to place the merged document into the correct folder using 'fldrname'_

    if the merged document has just one resulting document.

    Q: Is it possible to save the individual documents to the correct folders if the merge action results in more than one document.

    I.e. when I send out Guarantees for the work that we do, I do this in small batches of say 10 customers and the merge action

    creates a single document with all ten guarantee pages which are then sent to printer.

    I would like the Guarantee documents to be copied to each of the customers folders individually, although the final result for printing

    is fine in one document of ten pages.

    Many thanks in advance for any suggestions.

    Andy

    Sunday, June 20, 2010 10:39 AM

Answers

  • The following modified code should get rid of the blank page by setting the start of the second section to Continuous

    Sub SplitMergeLetter()
    ' splitter Macro modified to save individual letters with
    ' information from data source. The filename data must be added to
    ' the top of the merge letter - see web article.

    Selection.EndKey Unit:=wdStory
    Letters = Selection.Information(wdActiveEndSectionNumber)
    Selection.HomeKey Unit:=wdStory
    Counter = 1
    While Counter < Letters
        Application.ScreenUpdating = False
        With Selection
           .HomeKey Unit:=wdStory
           .EndKey Unit:=wdLine, Extend:=wdExtend
           .MoveLeft Unit:=wdCharacter, Count:=1, Extend:=wdExtend
        End With
        fldrname = Selection
        Docname = "C:\Clean DataBase\WdQuotes\" & fldrname & "\" & "Guarantee.Doc"
        ActiveDocument.Sections.First.Range.Cut
        Documents.Add
        With Selection
            .Paste
            .HomeKey Unit:=wdStory
            .MoveDown Unit:=wdLine, Count:=1, Extend:=wdExtend
            .Delete
        End With
        With ActiveDocument
            .Sections(2).PageSetup.SectionStart = wdSectionContinuous
            .SaveAs FileName:=Docname, _
            FileFormat:=wdFormatDocument
        End With
        ActiveWindow.Close
        Counter = Counter + 1
        Application.ScreenUpdating = True
    Wend

    End Sub


    -- Hope this helps.

    Doug Robbins - Word MVP,
    dkr[atsymbol]mvps[dot]org
    Posted via the Community Bridge

    "Andywal6" wrote in message news:7d1fe0d3-cf1a-41c6-96e1-865d863058ed@communitybridge.codeplex.com...

    Nearly there Doug.

    I took the code out of the template guarantee doc and pasted it into a macro on the Normal document template but still called it from the last instruction in my guarantee code as I did before and it works correctly, although it, for some inexplicable reason creates a second blank page to the saved documents.

    Best regards and many thanks

    Andy


    Doug Robbins - Word MVP dkr[atsymbol]mvps[dot]org
    • Marked as answer by Bessie Zhao Thursday, June 24, 2010 6:30 AM
    Monday, June 21, 2010 10:09 PM

All replies

  • See the “Individual Merge Letters” item on fellow MVP Graham Mayor’s website at:

    http://www.gmayor.com/individual_merge_letters.htm

    If you are using Word XP or later, the “Add-in to Merge Letters to Separate Files” that I have written and that can be downloaded from that site will allow you to create each letter as a separate file with a filename taken from a field in the data source with a minimum of fuss.


    -- Hope this helps.

    Doug Robbins - Word MVP,
    dkr[atsymbol]mvps[dot]org
    Posted via the Community Bridge

    "Andywal6" wrote in message news:6946ca43-7d9a-426c-9bf3-9f069bbedad1@communitybridge.codeplex.com...

    Ref my earlier question:

    http://social.msdn.microsoft.com/Forums/en-US/worddev/thread/8d232bf2-3325-4049-897d-7da30b8e26e6

    Thanks to Doug for his help..

    Private Sub Document_Open()
    Dim fldrname As String
    With ActiveDocument.MailMerge
        Destination = wdSendToNewDocument
        MailAsAttachment = False
        MailAddressFieldName = ""
        MailSubject = ""
        SuppressBlankLines = True
        With .DataSource
            .ActiveRecord = wdFirstRecord
            fldrname = .DataFields("FolderNo").Value
            FirstRecord = wdDefaultFirstRecord
            LastRecord = wdDefaultLastRecord
        End With
        Execute Pause:=True
    End With
    ActiveDocument.SaveAs FileName:="C:\My DataBase\WordQuotes\" & fldrname & "\Thank You Letter.doc"
    End Sub

    The line starting..'ActiveDocument.SaveAs' works great to place the merged document into the correct folder using 'fldrname'_

    if the merged document has just one resulting document.

    Q: Is it possible to save the individual documents to the correct folders if the merge action results in more than one document.

    I.e. when I send out Guarantees for the work that we do, I do this in small batches of say 10 customers and the merge action

    creates a single document with all ten guarantee pages which are then sent to printer.

    I would like the Guarantee documents to be copied to each of the customers folders individually, although the final result for printing

    is fine in one document of ten pages.

    Many thanks in advance for any suggestions.

    Andy


    Doug Robbins - Word MVP dkr[atsymbol]mvps[dot]org
    Sunday, June 20, 2010 10:47 PM
  • Hi Doug

    Thank you again for your invaluable help.

    But I have a problem that I just can't see no matter how long I look.

    I have revised my code to the code below and the first Letter is cut-pasted and saved into the correct folder

    under the 'fldrname' inserted at the top of my merge template and inserted into the 'Docname' and path.

    but then it stops and the second letter is not created or saved.

    Also an 'Unknown Error' is thrown within Access. (Thanks M-soft for being so helpful)

    Any thoughts would be appreciated.

    Best regards Andy

    '***********************************************************************

    Private Sub Document_Open()

        With ActiveDocument.MailMerge
            .Destination = wdSendToNewDocument
            .MailAsAttachment = False
            .MailAddressFieldName = ""
            .MailSubject = ""
            .SuppressBlankLines = True
            With .DataSource
                .FirstRecord = wdDefaultFirstRecord
                .LastRecord = wdDefaultLastRecord
            End With
            .Execute Pause:=True
        End With
    Call SplitMergeLetter
    End Sub


    '**************************************************************************************

    Sub SplitMergeLetter()
    ' splitter Macro modified to save individual letters with
    ' information from data source. The filename data must be added to
    ' the top of the merge letter - see web article.

    Selection.EndKey Unit:=wdStory
    Letters = Selection.Information(wdActiveEndSectionNumber)
    Selection.HomeKey Unit:=wdStory
    Counter = 1
    While Counter < Letters
         Application.ScreenUpdating = False


         With Selection
            .HomeKey Unit:=wdStory
            .EndKey Unit:=wdLine, Extend:=wdExtend
            .MoveLeft Unit:=wdCharacter, Count:=1, Extend:=wdExtend
         End With

         fldrname = Selection
         Docname = "C:\Clean DataBase\WdQuotes\" & fldrname & "\" & "Guarantee.Doc"

         ActiveDocument.Sections.First.Range.Cut
         Documents.Add

         With Selection
             .Paste
             .HomeKey Unit:=wdStory
             .MoveDown Unit:=wdLine, Count:=1, Extend:=wdExtend
             .Delete
         End With
        
         ActiveDocument.SaveAs FileName:=Docname, _
         FileFormat:=wdFormatDocument
         ActiveWindow.Close
         Counter = Counter + 1
         Application.ScreenUpdating = True
    Wend

    End Sub

    Monday, June 21, 2010 10:16 AM
  • Try running the SplitMergeLetter macro as a separate macro on the document that is created by executing the merge.  I am not sure that if you run it by using the Call statement in the macro that executes the merge that the active document will be the document created by executing the merge or the mail merge main document.

    -- Hope this helps.

    Doug Robbins - Word MVP,
    dkr[atsymbol]mvps[dot]org
    Posted via the Community Bridge

    "Andywal6" wrote in message news:53d96f63-256f-4236-800f-9b7543b5bd75@communitybridge.codeplex.com...

    Hi Doug

    Thank you again for your invaluable help.

    But I have a problem that I just can't see no matter how long I look.

    I have revised my code to the code below and the first Letter is cut-pasted and saved into the correct folder

    under the 'fldrname' inserted at the top of my merge template and inserted into the 'Docname' and path.

    but then it stops and the second letter is not created or saved.

    Also an 'Unknown Error' is thrown within Access. (Thanks M-soft for being so helpful)

    Any thoughts would be appreciated.

    Best regards Andy

    '***********************************************************************

    Private Sub Document_Open()

        With ActiveDocument.MailMerge
            .Destination = wdSendToNewDocument
            .MailAsAttachment = False
            .MailAddressFieldName = ""
            .MailSubject = ""
            .SuppressBlankLines = True
            With .DataSource
                .FirstRecord = wdDefaultFirstRecord
                .LastRecord = wdDefaultLastRecord
            End With
            .Execute Pause:=True
        End With
    Call SplitMergeLetter
    End Sub


    '**************************************************************************************

    Sub SplitMergeLetter()
    ' splitter Macro modified to save individual letters with
    ' information from data source. The filename data must be added to
    ' the top of the merge letter - see web article.

    Selection.EndKey Unit:=wdStory
    Letters = Selection.Information(wdActiveEndSectionNumber)
    Selection.HomeKey Unit:=wdStory
    Counter = 1
    While Counter < Letters
         Application.ScreenUpdating = False


         With Selection
            .HomeKey Unit:=wdStory
            .EndKey Unit:=wdLine, Extend:=wdExtend
            .MoveLeft Unit:=wdCharacter, Count:=1, Extend:=wdExtend
         End With

         fldrname = Selection
         Docname = "C:\Clean DataBase\WdQuotes\" & fldrname & "\" & "Guarantee.Doc"

         ActiveDocument.Sections.First.Range.Cut
         Documents.Add

         With Selection
             .Paste
             .HomeKey Unit:=wdStory
             .MoveDown Unit:=wdLine, Count:=1, Extend:=wdExtend
             .Delete
         End With

         ActiveDocument.SaveAs FileName:=Docname, _
         FileFormat:=wdFormatDocument
         ActiveWindow.Close
         Counter = Counter + 1
         Application.ScreenUpdating = True
    Wend

    End Sub


    Doug Robbins - Word MVP dkr[atsymbol]mvps[dot]org
    Monday, June 21, 2010 11:48 AM
  • Nearly there Doug.

    I took the code out of the template guarantee doc and pasted it into a macro on the Normal document template but still called it from the last instruction in my guarantee code as I did before and it works correctly, although it, for some inexplicable reason creates a second blank page to the saved documents.

    Best regards and many thanks

    Andy

    Monday, June 21, 2010 1:38 PM
  • The following modified code should get rid of the blank page by setting the start of the second section to Continuous

    Sub SplitMergeLetter()
    ' splitter Macro modified to save individual letters with
    ' information from data source. The filename data must be added to
    ' the top of the merge letter - see web article.

    Selection.EndKey Unit:=wdStory
    Letters = Selection.Information(wdActiveEndSectionNumber)
    Selection.HomeKey Unit:=wdStory
    Counter = 1
    While Counter < Letters
        Application.ScreenUpdating = False
        With Selection
           .HomeKey Unit:=wdStory
           .EndKey Unit:=wdLine, Extend:=wdExtend
           .MoveLeft Unit:=wdCharacter, Count:=1, Extend:=wdExtend
        End With
        fldrname = Selection
        Docname = "C:\Clean DataBase\WdQuotes\" & fldrname & "\" & "Guarantee.Doc"
        ActiveDocument.Sections.First.Range.Cut
        Documents.Add
        With Selection
            .Paste
            .HomeKey Unit:=wdStory
            .MoveDown Unit:=wdLine, Count:=1, Extend:=wdExtend
            .Delete
        End With
        With ActiveDocument
            .Sections(2).PageSetup.SectionStart = wdSectionContinuous
            .SaveAs FileName:=Docname, _
            FileFormat:=wdFormatDocument
        End With
        ActiveWindow.Close
        Counter = Counter + 1
        Application.ScreenUpdating = True
    Wend

    End Sub


    -- Hope this helps.

    Doug Robbins - Word MVP,
    dkr[atsymbol]mvps[dot]org
    Posted via the Community Bridge

    "Andywal6" wrote in message news:7d1fe0d3-cf1a-41c6-96e1-865d863058ed@communitybridge.codeplex.com...

    Nearly there Doug.

    I took the code out of the template guarantee doc and pasted it into a macro on the Normal document template but still called it from the last instruction in my guarantee code as I did before and it works correctly, although it, for some inexplicable reason creates a second blank page to the saved documents.

    Best regards and many thanks

    Andy


    Doug Robbins - Word MVP dkr[atsymbol]mvps[dot]org
    • Marked as answer by Bessie Zhao Thursday, June 24, 2010 6:30 AM
    Monday, June 21, 2010 10:09 PM
  • Hi Doug.  I downloaded the macro from gmayer.  but when I run it, it makes my document twice as long by making the spacing double.   like double return carriage.  any way to change this?
    Monday, November 15, 2010 9:49 PM
  • Was it the split merge macro or the add-in that you used?

    If the add-in, the change in the spacing results from the formatting in the normal template.

    -- Hope this helps.

    Doug Robbins - Word MVP,
    dkr[atsymbol]mvps[dot]org
    Posted via the Community Bridge

    "joe schmo" wrote in message news:24bdd0f6-fd39-475c-803d-1c5b4e95664a@communitybridge.codeplex.com...

    Hi Doug.  I downloaded the macro from gmayer.  but when I run it, it makes my document twice as long by making the spacing double.   like double return carriage.  any way to change this?


    Doug Robbins - Word MVP dkr[atsymbol]mvps[dot]org
    Tuesday, November 16, 2010 12:28 AM
  • Any changes in spacing are attributable to the template used to create the separate documents. The add-in thus includes a function to create a template from the document which will eliminate the problem.



    <joe schmo> wrote in message news:24bdd0f6-fd39-475c-803d-1c5b4e95664a@communitybridge.codeplex.com...

    Hi Doug.  I downloaded the macro from gmayer.  but when I run it, it makes my document twice as long by making the spacing double.   like double return carriage.  any way to change this?


    Graham Mayor - Word MVP
    www.gmayor.com
    Posted via the Communities Bridge
    http://communitybridge.codeplex.com/
    Tuesday, November 16, 2010 6:44 AM