PDA

View Full Version : Copy Word Files including Header/Footer... add Page Break



Philcjr
11-29-2016, 09:13 AM
WORD VBA Ninjas,

I need a little help tweaking this code, that is to copy all Word (.docx) files into a single Word Document.

Here is my code that currently works, in so far as, copying and pasting the files... it is not copying the Headers nor Footers and I need a Page Break after each file.



Sub CombineWordFiles()


Dim FinalDoc As Document, SourceDoc As Document
Dim sFile As String


Const sFolder = "C:\Users\collinp4\Desktop\WordFile\"


Application.ScreenUpdating = False

Set FinalDoc = Documents.Add


sFile = Dir(sFolder & "*.docx")

Do While sFile <> ""
Set SourceDoc = Application.Documents.Open(sFolder & sFile)

With Application
.Selection.WholeStory
.Selection.Copy
.ActiveWindow.Close savechanges:=wdDoNotSaveChanges
.Selection.PasteAndFormat (wdFormatOriginalFormatting)
End With

' FinalDoc.InsertBreak (Type:=wdPageBreak) <=== This does not work?


sFile = Dir
Loop

Application.ScreenUpdating = True

MsgBox ("Files are merged")


End Sub


Thank You for your time and help,