UPDATE: After an afternoon of Googling, I've managed to cobble out a new code that works significantly better! It's as below:
Sub SplitChapterByHeading()
Application.ScreenUpdating = False
Dim Rng As Range
Dim HeadingName As Integer
' The name of the heading to use as base
Dim Msg As String
' This is what to display on the dialog box
Dim FilePath As String
FilePath = ActiveDocument.Path
'PlayTheSound "W21 - Awaiting Orders.wav"
Msg = "Which Heading to use as base (NUMBER ONLY)?"
HeadingName = InputBox(Msg)
Application.DisplayAlerts = False
'PlayTheSound "W22 - As You Requested.wav"
With ActiveDocument.Range
Do
With .Find
.ClearFormatting
.Replacement.ClearFormatting
.Text = ""
.Style = "Heading " & HeadingName
.Format = True
.Forward = True
.MatchCase = True
.Wrap = wdFindStop
.MatchWildcards = False
.Execute
End With
If .Find.Found = True Then
Set Rng = .Duplicate.GoTo(What:=wdGoToBookmark, Name:="\HeadingLevel")
Rng.Select
Selection.Copy
Documents.Add
Selection.Paste
ActiveDocument.SaveAs FileName:=FilePath & "\Splited Document\chap.docx"
ActiveDocument.Close
End If
Loop While .Find.Found
End With
Application.ScreenUpdating = True
End Sub
Basically, it search through the whole document, looking for any passage using a specific heading style, then copy it along with everything underneath to a new document, and save it. I don't fully get the logic behind it, but it seems to do the job pretty well.
However, now I ran into another problem: this macro will make every save the same file, overriding each other. At the end of the day, there's only 1 "chapter.docx" file, and it contain the very last section the macro copied.
Is there a way for it to save every find into a separate file, with each having a sequence number, such as "chapter (1).docx," "chapter (2).docx," etc.?