Split master document in two smaller documents

SmallFry
StarLounger
Posts: 91
Joined: 02 Sep 2018, 23:12

Split master document in two smaller documents

Post by SmallFry »

I have a master document which I would like to keep intact, but would like to make two smaller files from the master. So in the end there would be 3 files in the folder. Master, Report, and Final.

The spot to split will very from document to document, so I was thinking for a prompt to ask the user for the ending page, and the beginning would naturally be page 1.

This document should be saved as Report.docx and it can be in the path of the master document.

The second document would simply be from the page after the last page of the first document to the end of the master document. That file can be saved as Final.docx.

Can I get some help on the VBA approach for this?

With a 22 page master document, the Report.docx could be page 1 thru 10 (user entered) and the Final.docx could be 11 thru 22.

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

Re: Split master document in two smaller documents

Post by HansV »

Here is a macro:

Code: Select all

Sub SplitDocument()
    Dim docOld As Document
    Dim docNew As Document
    Dim strPath As String
    Dim p As Long
    Dim n As Long
    Dim s As Long
    Dim m As Long
    Set docOld = ActiveDocument
    strPath = docOld.Path
    n = docOld.ComputeStatistics(wdStatisticPages)
    p = Val(InputBox("How many pages in the first (report) document?"))
    If p <= 0 Or p >= n Then
        Beep
        Exit Sub
    End If
    Application.ScreenUpdating = False
    Selection.GoTo What:=wdGoToPage, Which:=wdGoToAbsolute, Count:=p + 1
    s = Selection.Start
    docOld.Range(0, s).Copy
    Set docNew = Documents.Add
    docNew.Content.Paste
    docNew.SaveAs2 FileName:=strPath & "\Report.docx"
    docNew.Close SaveChanges:=False
    m = docOld.Content.End
    docOld.Range(s, m).Copy
    Set docNew = Documents.Add
    docNew.Content.Paste
    docNew.SaveAs2 FileName:=strPath & "\Final.docx"
    docNew.Close SaveChanges:=False
    Application.ScreenUpdating = True
End Sub
Best wishes,
Hans

SmallFry
StarLounger
Posts: 91
Joined: 02 Sep 2018, 23:12

Re: Split master document in two smaller documents

Post by SmallFry »

Thanks again for your time Hans. I will try this today at work.

SmallFry
StarLounger
Posts: 91
Joined: 02 Sep 2018, 23:12

Re: Split master document in two smaller documents

Post by SmallFry »

Hi Hans,

This work great, but I did make one slight tweak.

Changed

Code: Select all

strPath = docOld.Path
To

Code: Select all

strPath = docOld.Path & Application.PathSeparator
The file was saving to one level above the actual path. This fixed it for my needs.

User avatar
Charles Kenyon
5StarLounger
Posts: 615
Joined: 10 Jan 2016, 15:56
Location: Madison, Wisconsin

Re: Split master document in two smaller documents

Post by Charles Kenyon »

Just a cautionary note. The Master Document feature is known for its ability to destroy work totally.
http://addbalance.com/word/masterdocuments.htm
Like a very sharp knife, it can have uses, but handle with care!

Edit: Re-reading, I see that you are likely not using this "feature."
You may also want to look at Graham Mayor's Boiler Add-In.
http://www.gmayor.com/Boiler.htm

SmallFry
StarLounger
Posts: 91
Joined: 02 Sep 2018, 23:12

Re: Split master document in two smaller documents

Post by SmallFry »

Thank you Charles for the words of caution.