Split document into multiple single-paged documents

yanlok1345
StarLounger
Posts: 74
Joined: 18 Oct 2023, 14:48

Split document into multiple single-paged documents

Post by yanlok1345 »

Hi all,

I've been using two macros that I found online to split a document into multiple single-paged documents.

However, the issue I'm facing is that each split document file has two pages, where the second page is blank, despite wanting each split document to have only one page. This is because the page break is saved in each split document.

Unfortunately, removing all the page breaks before running the macro isn't an option as it would alter the original document's formatting and page 2's content may partially move to page 1.

Do you know of any other ways to split a document into multiple single-paged documents where each split document only contains one page? Many thanks for your help!

The first one:

Code: Select all

Option Explicit 'This goes in the Declarations section of your code module.
 'Hopefully it is already there because you have ticked the 'Require Variable Declaration' _
checkbox. (Tools/Options, Editor tab.) 
 
 
Sub SplitIntoPages() 
    Dim docMultiple As Document 
    Dim docSingle As Document 
    Dim rngPage As Range 
    Dim iCurrentPage As Integer 
    Dim iPageCount As Integer 
    Dim strNewFileName As String 
     
    Application.ScreenUpdating = False 'Makes the code run faster and reduces screen _
    flicker a bit. 
    Set docMultiple = ActiveDocument 'Work on the active document _
    (the one currently containing the Selection) 
    Set rngPage = docMultiple.Range 'instantiate the range object
    iCurrentPage = 1 
     'get the document's page count
    iPageCount = docMultiple.Content.ComputeStatistics(wdStatisticPages) 
    Do Until iCurrentPage > iPageCount 
        If iCurrentPage = iPageCount Then 
            rngPage.End = ActiveDocument.Range.End 'last page (there won't be a next page)
        Else 
             'Find the beginning of the next page
             'Must use the Selection object. The Range.Goto method will not work on a page
            Selection.GoTo wdGoToPage, wdGoToAbsolute, iCurrentPage + 1 
             'Set the end of the range to the point between the pages
            rngPage.End = Selection.Start 
        End If 
        rngPage.Copy 'copy the page into the Windows clipboard
        Set docSingle = Documents.Add 'create a new document
        docSingle.Range.Paste 'paste the clipboard contents to the new document
         'remove any manual page break to prevent a second blank
        docSingle.Range.Find.Execute Findtext:="^m", ReplaceWith:="" 
         'build a new sequentially-numbered file name based on the original multi-paged file name and path
        strNewFileName = Replace(docMultiple.FullName, ".doc", "_" & Right$("000" & iCurrentPage, 4) & ".doc") 
        docSingle.SaveAs strNewFileName 'save the new single-paged document
        iCurrentPage = iCurrentPage + 1 'move to the next page
        docSingle.Close 'close the new document
        rngPage.Collapse wdCollapseEnd 'go to the next page
    Loop 'go to the top of the do loop
    Application.ScreenUpdating = True 'restore the screen updating
     
     'Destroy the objects.
    Set docMultiple = Nothing 
    Set docSingle = Nothing 
    Set rngPage = Nothing 
End Sub 
The second one:

Code: Select all

Sub SaveEachPageAsDoc()

    Dim doc As Document
    Dim rngPage As Range
    Dim newDoc As Document
    Dim pageCount As Integer
    Dim savePath As String
    
    Set doc = ActiveDocument
    
    With Application.FileDialog(msoFileDialogFolderPicker)
        .Title = "Select a Folder to Save Individual Page Documents"
        If .Show = -1 Then
            savePath = .SelectedItems(1) & Application.PathSeparator
        Else
            Exit Sub
        End If
    End With

    For pageCount = 1 To doc.ComputeStatistics(wdStatisticPages)

        Set rngPage = doc.GoTo(What:=wdGoToPage, Which:=wdGoToAbsolute, Count:=pageCount).GoTo(What:=wdGoToBookmark, Name:="\page")
        Set newDoc = Documents.Add
        
        rngPage.Copy
        newDoc.Range.Paste
        newDoc.Range.Font.Name = "Times New Roman"
        newDoc.SaveAs2 savePath & "Page " & pageCount & ".docx"
        newDoc.Close SaveChanges:=wdDoNotSaveChanges
        
    Next pageCount

End Sub

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

Re: Split document into multiple single-paged documents

Post by HansV »

Does this work?

Code: Select all

Sub SaveEachPageAsDoc()

    Dim doc As Document
    Dim rngPage As Range
    Dim newDoc As Document
    Dim pageCount As Integer
    Dim savePath As String
    
    Set doc = ActiveDocument
    
    With Application.FileDialog(msoFileDialogFolderPicker)
        .Title = "Select a Folder to Save Individual Page Documents"
        If .Show = -1 Then
            savePath = .SelectedItems(1) & Application.PathSeparator
        Else
            Exit Sub
        End If
    End With

    For pageCount = 1 To doc.ComputeStatistics(wdStatisticPages)

        Set rngPage = doc.GoTo(What:=wdGoToPage, Which:=wdGoToAbsolute, Count:=pageCount).GoTo(What:=wdGoToBookmark, Name:="\page")
        Set newDoc = Documents.Add
        
        rngPage.Copy
        newDoc.Range.Paste
        newDoc.Range.Font.Name = "Times New Roman"
        newDoc.Range.Find.Execute Findtext:="^m", ReplaceWith:="", Replace:=wdReplaceAll
        newDoc.SaveAs2 savePath & "Page " & pageCount & ".docx"
        newDoc.Close SaveChanges:=wdDoNotSaveChanges
        
    Next pageCount

End Sub
Best wishes,
Hans

yanlok1345
StarLounger
Posts: 74
Joined: 18 Oct 2023, 14:48

Re: Split document into multiple single-paged documents

Post by yanlok1345 »

HansV wrote:
10 Feb 2024, 15:36
Does this work?

Code: Select all

Sub SaveEachPageAsDoc()

    Dim doc As Document
    Dim rngPage As Range
    Dim newDoc As Document
    Dim pageCount As Integer
    Dim savePath As String
    
    Set doc = ActiveDocument
    
    With Application.FileDialog(msoFileDialogFolderPicker)
        .Title = "Select a Folder to Save Individual Page Documents"
        If .Show = -1 Then
            savePath = .SelectedItems(1) & Application.PathSeparator
        Else
            Exit Sub
        End If
    End With

    For pageCount = 1 To doc.ComputeStatistics(wdStatisticPages)

        Set rngPage = doc.GoTo(What:=wdGoToPage, Which:=wdGoToAbsolute, Count:=pageCount).GoTo(What:=wdGoToBookmark, Name:="\page")
        Set newDoc = Documents.Add
        
        rngPage.Copy
        newDoc.Range.Paste
        newDoc.Range.Font.Name = "Times New Roman"
        newDoc.Range.Find.Execute Findtext:="^m", ReplaceWith:="", Replace:=wdReplaceAll
        newDoc.SaveAs2 savePath & "Page " & pageCount & ".docx"
        newDoc.Close SaveChanges:=wdDoNotSaveChanges
        
    Next pageCount

End Sub
It works! I'm extremely grateful for your assistance!