VBA Split word document every 5 pages of word document

Flora Edwards
Flora Edwards used Ask the Experts™
on
I have word document files, which i need to split each 5 pages to a separate file.

is this possible to be done  with VBA?
Comment
Watch Question

Do more with

Expert Office
EXPERT OFFICE® is a registered trademark of EXPERTS EXCHANGE®
Shaun VermaakTechnical Specialist
Awarded 2017
Distinguished Expert 2018

Commented:
Multiple options here and VBA sample. First try split
Enterprise > Split
split.png
Top Expert 2016
Commented:
Hi,

pls try
Sub Macro()
strPath = "c:\myFolder"
LstPage = ActiveDocument.Range.Information(wdNumberOfPagesInDocument)
For idx1 = LstPage - (LstPage Mod 5) To 5 Step -5
    Selection.GoTo What:=wdGoToPage, Which:=wdGoToNext, Name:=CStr(idx1 + 1)
    Selection.InsertBreak Type:=wdSectionBreakContinuous
Next
For Idx2 = ActiveDocument.Sections.Count To 1 Step -1
    Documents.Add
    ThisDocument.Range.Copy
    ActiveDocument.Range.Paste
    For idx3 = ThisDocument.Sections.Count To 1 Step -1
        If Idx2 <> idx3 Then ActiveDocument.Sections(idx3).Range.Delete
    Next
    ActiveDocument.SaveAs strPath & "\" & "FileTest" & Idx2 & ".docm"
    ActiveDocument.Close False
Next
End Sub

Open in new window

Regards
Flora EdwardsMedicine

Author

Commented:
thanks very much

Do more with

Expert Office
Submit tech questions to Ask the Experts™ at any time to receive solutions, advice, and new ideas from leading industry professionals.

Start 7-Day Free Trial