Link to home
Start Free TrialLog in
Avatar of kamur
kamur

asked on

Split a mail-merged document into one page word documents each

Hello,

I have a mail-merge process where I insert 4 fields into a word document. The mail-merged document is usually one big word document close to 2,000 pages. I want to split this large word document into one page document each and the name of each document should be the value of the first field among the 4 fields that I am inserting.

That is, I am inserting 4 fields such as: client number, client name, client address and client phone in the mail-merge process. Therefore, the name of each split one page documents needs to be the respective client number value that is inserted for that page. (client number.doc).

Please advise how to do this

Thanks

Kamur
Avatar of GrahamSkan
GrahamSkan
Flag of United Kingdom of Great Britain and Northern Ireland image

It's not a problem splitting the document, but locating the first field might be. Mail merge resolves virtually all fields to their results and removes any bookmarks. If the data is in a table, we might be able to locate it by cell. Alternatively we could look for it in relation to some distinctive fixed text. The way that this code works is to ask the user to select the first occurence of the resulting text and to look for the first word in each section that is the same distance from the beginning of the section.

Sub SplitMergeResult()
    Dim sec As Section
    Dim rng As Range
    Dim strName As String
    Dim DocA As Document
    Dim DocB As Document
    Dim rngStart As Integer
   
    If Selection.Sections(1).Index <> 1 Then
        MsgBox "Please select the first name and restart this macro"
        End
    End If
    rngStart = Selection.Start
    Debug.Print rngStart
    Set DocA = ActiveDocument
    For Each sec In DocA.Sections
        Set rng = sec.Range
        rng.Start = sec.Range.Start + rngStart
        strName = Trim$(rng.Words(1).Text)
        Set DocB = Documents.Add
        DocB.SaveAs strName & ".doc"
        Debug.Print strName
        DocB.Close
    Next sec
End Sub
I was so concentrating on the document name this morning that I forgot about what to save. I'll come back soon with a better macro.
ASKER CERTIFIED SOLUTION
Avatar of GrahamSkan
GrahamSkan
Flag of United Kingdom of Great Britain and Northern Ireland image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial