Splitting mail merge result at specific text

User avatar
Rudi
gamma jay
Posts: 25455
Joined: 17 Mar 2010, 17:33
Location: Cape Town

Splitting mail merge result at specific text

Post by Rudi »

Hi,

I have the following macro that splits a mail merge result at the starting text for each iteration. (I use start text and not section break as the document already has multiple section breaks, so this condition will not work.)

Anyways, the macro below works great except that when it adds a new document and pastes the data, the document structure is slightly out as line spacing is different and one or two other small details.

I was thinking if it was not possible to instead of opening a new document and copying to it, to rather save a copy of the original first and then delete data before and after the find variables? In other words, it will enable one to keep the styles and formatting of the original document for each duplication, preserving the data for that iteration with the exact styling and formatting.

Is it possible to tweak this macro to do this?

Code: Select all

Sub RunSplitDoc()
    Dim docS As Document
    Dim docT As Document
    Dim myFind As String
    Dim i As Long
    Dim lngStart As Long
    Dim lngEnd As Long
    Dim wdDoc As Word.Document
    Dim FileName As String
    Dim PointPos As Long

    Selection.HomeKey Unit:=wdStory
    myFind = InputBox("Supply the text string to find that indicates " & _
        "where the document must be split.", "Split Position")
    If myFind = "" Then
        Exit Sub
    End If
    Application.ScreenUpdating = False
    Set docS = ActiveDocument
    ' Get name of source document
    FileName = docS.Name
    ' Get position of last . in file name
    PointPos = InStrRev(FileName, ".")
    ' Extract part before the point
    FileName = Left(FileName, PointPos - 1)
    i = -1
    Selection.HomeKey Unit:=wdStory
    With Selection.Find
        .ClearFormatting
        .Text = myFind
        .Wrap = wdFindStop
        Do While .Execute
            i = i + 1
            lngStart = lngEnd
            lngEnd = Selection.Start
            If i > 0 Then
                Set docT = Documents.Add
                docS.Range(Start:=lngStart, End:=lngEnd).Copy
                docT.Content.Paste
                ' Use original file name + suffix
                docT.SaveAs FileName:=docS.Path & "\" & FileName & Format(i, " 000")
                docT.Close
            End If
        Loop
    End With
    i = i + 1
    lngStart = lngEnd
    lngEnd = docS.Content.End
    Set docT = Documents.Add
    docS.Range(Start:=lngStart, End:=lngEnd).Copy
    docT.Content.Paste
    ' Use original file name + suffix
    docT.SaveAs FileName:=docS.Path & "\" & FileName & Format(i, " 000")
    docT.Close
    Selection.HomeKey Unit:=wdStory
    Application.ScreenUpdating = True
    MsgBox "Data splitting is complete. The split documents are in the same " & _
        "place as this current document.", vbInformation
End Sub
Regards,
Rudi

If your absence does not affect them, your presence didn't matter.

User avatar
HansV
Administrator
Posts: 78920
Joined: 16 Jan 2010, 00:14
Status: Microsoft MVP
Location: Wageningen, The Netherlands

Re: Splitting mail merge result at specific text

Post by HansV »

Does it help if you change the line

Code: Select all

                Set docT = Documents.Add
to

Code: Select all

                Set docT = Documents.Add(docS.FullName)
Best wishes,
Hans

User avatar
Rudi
gamma jay
Posts: 25455
Joined: 17 Mar 2010, 17:33
Location: Cape Town

Re: Splitting mail merge result at specific text

Post by Rudi »

Wow! It certainly does!

Does this line "duplicate" the original document template?
Regards,
Rudi

If your absence does not affect them, your presence didn't matter.

User avatar
HansV
Administrator
Posts: 78920
Joined: 16 Jan 2010, 00:14
Status: Microsoft MVP
Location: Wageningen, The Netherlands

Re: Splitting mail merge result at specific text

Post by HansV »

It creates a new document using the original document as if it were a template. So it effectively duplicates the original document including its content and formatting. The line

docT.Content.Paste

overwrites the contents.
Best wishes,
Hans

User avatar
Rudi
gamma jay
Posts: 25455
Joined: 17 Mar 2010, 17:33
Location: Cape Town

Re: Splitting mail merge result at specific text

Post by Rudi »

It's so easy if you know how! :smile:

TX again! It works a charm!
Regards,
Rudi

If your absence does not affect them, your presence didn't matter.

User avatar
macropod
4StarLounger
Posts: 508
Joined: 17 Dec 2010, 03:14

Re: Splitting mail merge result at specific text

Post by macropod »

Rudi wrote:I have the following macro that splits a mail merge result at the starting text for each iteration. (I use start text and not section break as the document already has multiple section breaks, so this condition will not work.)
See Split Merged Output to Separate Documents in the Mailmerge Tips and Tricks threads at:
http://www.msofficeforums.com/mail-merg ... ricks.html" onclick="window.open(this.href);return false;
or:
http://windowssecrets.com/forums/showth ... amp-Tricks" onclick="window.open(this.href);return false;
That macro allows you to nominate the number of Sections per record. Hence, you're not tied to a particular 'start text'. Better still would be to use the macro under Send Mailmerge Output to Individual Files there.
Paul Edstein
[Fmr MS MVP - Word]

User avatar
Rudi
gamma jay
Posts: 25455
Joined: 17 Mar 2010, 17:33
Location: Cape Town

Re: Splitting mail merge result at specific text

Post by Rudi »

TX Paul.
PLENTY of superb advice and tips around Mail Merge in these links.
Certainly worth a bookmark. :cheers:
Regards,
Rudi

If your absence does not affect them, your presence didn't matter.