Sub AssembleBook()
Dim SubDocFile$, FolderPath$, Template$
Dim Counter&
Dim oFolder As FileDialog
Dim oBookmark As Bookmark
Dim oTOC As TableOfContents
'Create a dynamic array variable, and then declare its initial size
Dim DirectoryListArray() As String
ReDim DirectoryListArray(1000)
'Use the built-in Folder Picker dialog to choose the folder holding all the subdocuments
Set oFolder = Application.FileDialog(msoFileDialogFolderPicker)
With oFolder
.AllowMultiSelect = False
If .Show <> 0 Then
FolderPath$ = .SelectedItems(1)
Else
GoTo EndSub 'If you cancel out of the dialog, this will exit the macro
End If
End With
'Turn off display updating for a less distracting appearance
Application.ScreenUpdating = False
'Loop through all the files in the directory by using Dir$ function
SubDocFile$ = Dir$(FolderPath$ & Application.PathSeparator & "*.*")
Do While SubDocFile$ <> ""
DirectoryListArray(Counter) = SubDocFile$
SubDocFile$ = Dir$
Counter& = Counter& + 1
Loop
'Reset the size of the array without losing its values by using Redim Preserve
ReDim Preserve DirectoryListArray(Counter& - 1)
'The following will sort the subdocuments by their name. You can prepend the name with a number to create any order you want them to be in the master document.
WordBasic.SortArray DirectoryListArray()
'Start the master document process
ActiveWindow.ActivePane.View.Type = wdOutlineView
ActiveWindow.View = wdMasterView
Selection.EndKey Unit:=wdStory
For x = 0 To (Counter& - 1)
If IsNumeric(Left(DirectoryListArray(x), 1)) Then
FullName$ = FolderPath$ & Application.PathSeparator & DirectoryListArray(x)
Documents.Open FileName:=FullName$, ConfirmConversions:=False
With Documents(FullName$)
.AttachedTemplate = Template$
For Each oBookmark In Documents(FullName$).Bookmarks
oBookmark.Delete
Next oBookmark
.Close SaveChanges:=True
End With
Selection.Range.Subdocuments.AddFromFile Name:=FullName$, ConfirmConversions:=False
End If
Next x
'Optional TOC update for a master document that already has a table of contents
' For Each oTOC In ActiveDocument.TablesOfContents
' oTOC.Update
' Next oTOC
'Switch back to Print Layout view and turn screen updating back on.
ActiveWindow.ActivePane.View.Type = wdPrintView
Application.ScreenUpdating = True
EndSub:
End Sub
You may need to do a few steps to convert it but there are several guides and tools out there that can help you through the process.
Editing e-books — calibre 5.19.0 documentation (calibre-ebook.com)
If you wanted to keep in in word though the process would be a matter of combining them into a single document then creating a table of contents to link to the specific sections.
Insert a table of contents - Office Support (microsoft.com)