Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 284
  • Last Modified:

MS Word- creat new file with every hard page break

I have a large mail merge and I need to create a new file from every hard page break that was inserted after every record. The records are from 1 to 5 pages because they contain tables, but there is a hard page break at the end of every record merge and I need to make a new word file from each record merge. Thanks for the help!
0
bigjacobi
Asked:
bigjacobi
  • 5
  • 4
  • 3
  • +1
1 Solution
 
irudykCommented:
Take a look at the following link for an explanation as to how this could be done - http://homepage.swissonline.ch/cindymeister/mergfaq2.htm#SepFile
0
 
bigjacobiAuthor Commented:
Hey irudyk,

Its still not working correctly. I'll attach the first two records of the merge so you can see what I'm trying to do. Its a merge from Access to word. I would use the macro that you did for me earlier but these merges are different numbers of pages. (btw thanks again for the other help, the macro works great!) So at the end of the record merge i need to split and create a new file for each one.
mergesplit.doc
0
 
GrahamSkanRetiredCommented:
Luckily it is actually a Next Page Section Break, and there is a Sections Collection, so a macro to do the job is fairly simple.
Sub SplitMergeResult()
    Dim sec As Section
    Dim rng As Range
    Dim strName As String
    Dim DocA As Document
    Dim DocB As Document
    
    Set DocA = ActiveDocument
    For Each sec In DocA.Sections
        Set rng = sec.Range
        If rng.End < DocA.Range.End Then
            rng.MoveEnd wdCharacter, -1
        End If
        strName = "MyDoc" & Format$(sec.Index, "000") & ".doc"
        Set DocB = Documents.Add
        DocB.SaveAs strName
        DocB.Close
    Next sec
End Sub

Open in new window

0
Hire Technology Freelancers with Gigs

Work with freelancers specializing in everything from database administration to programming, who have proven themselves as experts in their field. Hire the best, collaborate easily, pay securely, and get projects done right.

 
GrahamSkanRetiredCommented:
Your document is not the unedited result of a simple Word mailmerge (no section breaks), so my macro wouldn't work. However it also has the copy and paste missing. This is the correction.
Sub SplitMergeResult()
    Dim sec As Section
    Dim rng As Range
    Dim strName As String
    Dim DocA As Document
    Dim DocB As Document
    
    Set DocA = ActiveDocument
    For Each sec In DocA.Sections
        Set rng = sec.Range
        If rng.End < DocA.Range.End Then
            rng.MoveEnd wdCharacter, -1
        End If
        strName = "MyDoc" & Format$(sec.Index, "000") & ".doc"
        rng.Copy
        Set DocB = Documents.Add
        DocB.Range.Paste
        DocB.SaveAs strName
        DocB.Close
    Next sec
End Sub

Open in new window

0
 
GrahamSkanRetiredCommented:
This should split a document at manual page breaks
Sub SplitMergeResult()
    Dim i As Integer
    Dim rng As Range
    Dim strName As String
    Dim DocA As Document
    Dim DocB As Document
    Dim rngStart As Long
    Set DocA = ActiveDocument
    Set rng = DocA.Range
    With rng.Find
        .Text = "^m"
        i = 1
        rngStart = 0
       Do While .Execute
            rng.MoveEnd wdCharacter, -1
            rng.Start = rngStart
            rng.Copy
            Set DocB = Documents.Add
            strName = "MyDoc" & Format$(i, "000") & ".doc"
            DocB.Range.Paste
            DocB.SaveAs strName
            DocB.Close
            rng.Collapse wdCollapseEnd
            rng.Move wdCharacter, 1
            rngStart = rng.Start
            rng.End = DocA.Range.End
            i = i + 1
        Loop
        'last page
        rng.Copy
        Set DocB = Documents.Add
        strName = "MyDoc" & Format$(i, "000") & ".doc"
        DocB.Range.Paste
        DocB.SaveAs strName
        DocB.Close
    End With
End Sub

Open in new window

0
 
bigjacobiAuthor Commented:
Hey GrahamSkan,

I ran the macro and it just copied the whole document over to MyDoc001.doc
0
 
bigjacobiAuthor Commented:
Ok I'll try the newest revision and let you know what happens! Thanks!
0
 
bigjacobiAuthor Commented:
Hey GrahamSkan,

That last macro worked great! Thanks!!!


0
 
kdr2003Commented:
Hi GrahamSkan,
                             I am using your Macro above for a RTF document formatted in Landscape.  I run the macro but the new files are created as RTF but as portrait and each file needs to be adjusted to landscape and resetting margins to sell all information.   Can you help?

thks
Ken
0
 
bigjacobiAuthor Commented:
Hey kdr2003,

I added this line to make it landscape

DocB.PageSetup.Orientation = wdOrientLandscape


put it inbetween
20:     DocB.Range.Paste
21:     DocB.SaveAs strName


Sub SplitMergeResult()
    Dim i As Integer
    Dim rng As Range
    Dim strName As String
    Dim DocA As Document
    Dim DocB As Document
    Dim rngStart As Long
    Set DocA = ActiveDocument
    Set rng = DocA.Range
    With rng.Find
        .Text = "^m"
        i = 1
        rngStart = 0
       Do While .Execute
            rng.MoveEnd wdCharacter, -1
            rng.Start = rngStart
            rng.Copy
            Set DocB = Documents.Add
            strName = "MyDoc" & Format$(i, "000") & ".doc"
            DocB.Range.Paste
            DocB.PageSetup.Orientation = wdOrientLandscape
            DocB.SaveAs strName
            DocB.Close
            rng.Collapse wdCollapseEnd
            rng.Move wdCharacter, 1
            rngStart = rng.Start
            rng.End = DocA.Range.End
            i = i + 1
        Loop
        'last page
        rng.Copy
        Set DocB = Documents.Add
        strName = "MyDoc" & Format$(i, "000") & ".doc"
        DocB.Range.Paste
        DocB.SaveAs strName
        DocB.Close
    End With
End Sub

Open in new window

0
 
kdr2003Commented:
Thanks for info.  
0
 
kdr2003Commented:
Actually,  I got an error when I ran the macro at the new line.
0
 
GrahamSkanRetiredCommented:
What is the error message?

It might be a bit better to set the orientation before the paste.
0

Featured Post

Keep up with what's happening at Experts Exchange!

Sign up to receive Decoded, a new monthly digest with product updates, feature release info, continuing education opportunities, and more.

  • 5
  • 4
  • 3
  • +1
Tackle projects and never again get stuck behind a technical roadblock.
Join Now