Word Macro that combines files, need to insert breaks & titles

defyalllogic

i'm a girl. (i have tendonitis)
Kind Benefactor
Super Member
Registered
Joined
Mar 1, 2010
Messages
1,458
Reaction score
183
Location
Massachusetts
So I have a macro that i run to merge my chapters.
Sub MergeDocs()
Dim rng As Range
Dim MainDoc As Document
Dim strFile As String
Const strFolder = "C:\Book\Chapters\" 'change to suit
Set MainDoc = Documents.Add
strFile = Dir$(strFolder & "*.rtf") ' can change to .docx
Do Until strFile = ""
Set rng = MainDoc.Range
rng.Collapse wdCollapseEnd
rng.InsertFile strFolder & strFile
strFile = Dir$()
Loop
End Sub

However, I need to insert breaks that seperate the files so i know where one ends and the next begins. (they're chapters)

This on creates a blank page for each file but they're all at the start of the document. 13 files, 14 blank pages at the beginning of the document and the no differentiation....
Sub MergeDocs()
Dim rng As Range
Dim FinalEvidence As Document
Dim strFile As String
'Dim OutPut_file_name As String
Const strFolder = "C:\Test\" 'change to any dir which you wanna use
Set FinalEvidence = Documents.Add
strFile = Dir$(strFolder & "*.rtf")
Do Until strFile = ""
Set rng = FinalEvidence.Range
rng.Collapse wdCollapseEnd
rng.InsertFile strFolder & strFile
strFile = Dir$()
Selection.InsertBreak Type:=wdPageBreak
Loop
' OutPut_file_name = "FinalEvidence.doc"
End Sub

BEST CASE: I'd be able to merge the files, they'd keep their titles, and they'd clearly begin and end inside the document.

Please help?
Thanks!
 

Maryn

I Tried
Super Member
Registered
Joined
Feb 12, 2005
Messages
64,139
Reaction score
43,140
Location
Behind you!
You're making yourself quite a bit of work for no purpose.

You can merge files manually into a single document about as fast as your fingers can fly. You can either be sure each ends with a page break, or insert them manually after you're done.

I've put multiple chapters into a single whole-novel document in less than five minutes--and vowed I would never again keep each chapter as a separate file.

Maryn, big believer in simple
 

defyalllogic

i'm a girl. (i have tendonitis)
Kind Benefactor
Super Member
Registered
Joined
Mar 1, 2010
Messages
1,458
Reaction score
183
Location
Massachusetts
I like macros though, and I don't plan to just do it once...

the sucky thing is I had it before and lost it. :(
 

defyalllogic

i'm a girl. (i have tendonitis)
Kind Benefactor
Super Member
Registered
Joined
Mar 1, 2010
Messages
1,458
Reaction score
183
Location
Massachusetts
WOOP! I didn't loose it. I will share it.
Sub dofiles()


Set fs = Application.FileSearch
Dim newbook, curbook, MyCount, myrange
With fs
.lookin = "c:\"
'LookIn should be changed to the location you want to use
.SearchSubFolders = True 'assuming you DO want to search subfolders

.FileName = "*.doc"
'if you are looking for other than DOC files, alter the line above - so probably RTF in your case
If .Execute(SortBy:=msoSortByFileName, SortOrder:=msoSortOrderAscending) > 0 Then
For i = 1 To .FoundFiles.Count

Documents.Open FileName:=.FoundFiles(i)
Selection.WholeStory
Selection.Copy
ActiveWindow.Close
Selection.EndKey Unit:=wdStory

Selection.InsertBreak type:=wdSectionBreakNextPage
Selection.PasteAndFormat (wdPasteDefault)


Next i
Else
MsgBox "There were no files found."
End If
End With



End Sub

source
 

Jamesaritchie

Super Member
Registered
Joined
Feb 13, 2005
Messages
27,863
Reaction score
2,313
I still don't understand why you need a macro? Office does this better and faster.
 

jairey

Super Member
Registered
Joined
Feb 15, 2010
Messages
96
Reaction score
6
You can use styles to set your Chapter headings to automatically start on a new page.
 

defyalllogic

i'm a girl. (i have tendonitis)
Kind Benefactor
Super Member
Registered
Joined
Mar 1, 2010
Messages
1,458
Reaction score
183
Location
Massachusetts
i like macros because they're flexible and customizable. and office doesn't do it better or faster. it's just as fast once you do the code and customizable.

btw: i enjoy writing/learning code :)