Split Word Documents by Page with VBA While Preserving Headers
Question details
Split a Word document into separate files while retaining headers, footers, and page formatting.

- Product
- Microsoft Word 2007 or 2010
- Device & OS
- not provided
- Scenario
- A document needs to be divided by page, section, or heading into smaller Word files.
- Observed behavior
- The resulting files should retain the relevant headers, footers, and layout. A macro may report error 5941 if it references a section that does not exist.
Save the source document in a folder and check its section and header settings on the Page Layout tab. The macro below is intended for a single-section document with primary headers and footers, not different first-page or odd-and-even headers.
Run a VBA macro to split a single-section document by page
Use this macro to create one DOCX file per page without deleting content from the source document.
The macro copies formatted page content, primary headers and footers, and common page setup settings. It stops if the source has multiple sections or uses different first-page or odd-and-even headers.
Page boundaries can shift in the output, especially when a page begins in the middle of a paragraph or contains complex objects. Review each file after it is created.
Open and save the source document in Word, then press Alt+F11 and choose Insert > Module.
Paste the following code into the module: Sub SplitPagesWithHeaders() Dim src As Document, dst As Document Dim r As Range Dim starts() As Long Dim pages As Long, i As Long, endPos As Long Dim folder As String, outputFile As String Set src = ActiveDocument If src.Path = "" Then MsgBox "Save the source document first." Exit Sub End If If src.Sections.Count <> 1 Then MsgBox "This macro requires a single-section document." Exit Sub End If With src.Sections(1).PageSetup If .DifferentFirstPageHeaderFooter Or .OddAndEvenPagesHeaderFooter Then MsgBox "This macro supports primary headers and footers only." Exit Sub End If End With src.Repaginate pages = src.ComputeStatistics(wdStatisticPages) folder = src.Path & "\" ReDim starts(1 To pages) For i = 1 To pages Set r = src.GoTo(What:=wdGoToPage, Which:=wdGoToAbsolute, Count:=i) starts(i) = r.Start outputFile = folder & "SplitPage_" & i & ".docx" If Dir(outputFile) <> "" Then MsgBox "Output file already exists: " & outputFile Exit Sub End If Next i For i = 1 To pages If i < pages Then endPos = starts(i + 1) Else endPos = src.Content.End - 1 End If Set dst = Documents.Add With dst.Sections(1).PageSetup .Orientation = src.Sections(1).PageSetup.Orientation .PageWidth = src.Sections(1).PageSetup.PageWidth .PageHeight = src.Sections(1).PageSetup.PageHeight .TopMargin = src.Sections(1).PageSetup.TopMargin .BottomMargin = src.Sections(1).PageSetup.BottomMargin .LeftMargin = src.Sections(1).PageSetup.LeftMargin .RightMargin = src.Sections(1).PageSetup.RightMargin .HeaderDistance = src.Sections(1).PageSetup.HeaderDistance .FooterDistance = src.Sections(1).PageSetup.FooterDistance .Gutter = src.Sections(1).PageSetup.Gutter End With dst.Content.FormattedText = src.Range(starts(i), endPos).FormattedText dst.Sections(1).Headers(wdHeaderFooterPrimary).Range.FormattedText = _ src.Sections(1).Headers(wdHeaderFooterPrimary).Range.FormattedText dst.Sections(1).Footers(wdHeaderFooterPrimary).Range.FormattedText = _ src.Sections(1).Footers(wdHeaderFooterPrimary).Range.FormattedText outputFile = folder & "SplitPage_" & i & ".docx" dst.SaveAs FileName:=outputFile, FileFormat:=wdFormatXMLDocument dst.Close Next i MsgBox "Created " & pages & " files in " & folder End Sub
Return to the source document, press Alt+F8, select SplitPagesWithHeaders, and click Run. The files are saved in the source document's folder as SplitPage_1.docx, SplitPage_2.docx, and so on.
Open the new files and check their page breaks, headers, footers, images, and page numbers. Adjust the layout on the Page Layout tab if needed.

Fix error 5941 when a macro references a missing section
Use this check when debugging highlights a reference such as Target.Sections(2).
Review and edit split Word files with WPS Writer
WPS Office is a free, lightweight alternative with a familiar interface for opening, reviewing, and editing Word documents. Its format compatibility makes it easy to move existing files into your workflow; it does not fix Microsoft Word VBA errors.

Frequently Asked Questions
Why does Word show run-time error 5941 for Sections(2)?
The target document may contain only one section, so Sections(2) does not exist. In the VBA Immediate Window, check the target document's Sections.Count before accessing that section.
Will the macro preserve different first-page headers?
No. The example stops if Different First Page or different odd-and-even headers are enabled. Those layouts require additional handling for each header and footer type.
Can I split the document by heading instead of page?
Yes, but it requires a different macro that identifies heading paragraphs and copies the ranges between them. The example above uses Word's page locations and does not split by heading.
Why does a split file look different from the original page?
Copying content into a new document can change pagination, especially around paragraph breaks, tables, or floating objects. Open the output file and check its settings on the Page Layout tab.




