Try the following macro:
Code:
Sub CollateDocumentHeadings()
Application.ScreenUpdating = False
Dim strFolder As String, strFile As String, i As Long, Rng As Range
Dim wdDocSrc As Document, wdDocTmp As Document, wdDocOut As Document
strFolder = GetFolder
If strFolder = "" Then Exit Sub
Set wdDocTmp = Documents.Add
strFile = Dir(strFolder & "\*.docx", vbNormal)
While strFile <> ""
i = 0
If ThisDocument.FullName <> strFolder & "\" & strFile Then
Set wdDocSrc = Documents.Open(FileName:=strFolder & "\" & strFile, AddToRecentFiles:=False, Visible:=False)
With wdDocSrc
With .Range
With .Find
.ClearFormatting
.Replacement.ClearFormatting
.Text = ""
.Replacement.Text = ""
.Format = True
.Style = wdStyleHeading1
.Execute
End With
Do While .Find.Found
i = i + 1
Set Rng = .Duplicate
With Rng
.End = .End - 1
End With
With wdDocTmp
If .Sections.Count < i Then
.Range.InsertAfter vbCr
.Sections.Add Range:=.Characters.Last, Start:=wdSectionNewPage
End If
.Sections(i).Range.Characters.Last.InsertBefore Rng.Text & vbTab & strFile & vbCr
End With
.Collapse wdCollapseEnd
.Find.Execute
Loop
End With
.Close SaveChanges:=False
End With
End If
strFile = Dir()
Wend
With wdDocTmp
.SaveAs2 FileName:=strFolder & "\Headings Master List.docx", FileFormat:=wdFormatXMLDocument, AddToRecentFiles:=False
For i = 1 To .Sections.Count
Set Rng = .Sections(i).Range
With Rng
.MoveEnd wdCharacter, -1
End With
Set wdDocOut = Documents.Add(Visible:=False)
With wdDocOut
.Range.FormattedText = Rng.FormattedText
.SaveAs FileName:=strFolder & "\Headings (" & i & ") List.docx", FileFormat:=wdFormatXMLDocument, AddToRecentFiles:=False
.Close SaveChanges:=True
End With
Next
End With
Set wdDocSrc = Nothing: Set wdDocTmp = Nothing: Set wdDocOut = Nothing
Application.ScreenUpdating = True
End Sub
Function GetFolder() As String
Dim oFolder As Object
GetFolder = ""
Set oFolder = CreateObject("Shell.Application").BrowseForFolder(0, "Choose a folder", 0)
If (Not oFolder Is Nothing) Then GetFolder = oFolder.Items.Item.Path
Set oFolder = Nothing
End Function