Skip to content

Instantly share code, notes, and snippets.

@czmole
Last active March 2, 2023 13:16
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save czmole/eb70ff0f54053352ef466d5cfd9c8863 to your computer and use it in GitHub Desktop.
Save czmole/eb70ff0f54053352ef466d5cfd9c8863 to your computer and use it in GitHub Desktop.
Split Word Document by page in multiple files
' 1. Press Alt + F11 keys together to open the Microsoft Visual Basic for Application window;
' 2. Click Insert > Module, and then paste below VBA code into the new opening Module window.
' 3. Replace the starting point for iCurrentFile
' 4. Then click Run button or press F5 key to apply the VBA.
' Note: The splitting documents will be saved to the same place with the original file.
' Source: https://www.extendoffice.com/documents/word/966-word-split-documents-into-multiple-documents.html
Sub SplitIntoPages()
Dim docMultiple As Document
Dim docSingle As Document
Dim rngPage As Range
Dim iCurrentPage As Integer
Dim iCurrentFile 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
iCurrentFile = 2702
'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" & iCurrentFile, 4) & ".doc")
docSingle.SaveAs strNewFileName 'save the new single-paged document
iCurrentPage = iCurrentPage + 1 'move to the next page
iCurrentFile = iCurrentFile + 1
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
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment