PDA

View Full Version : Preserve formatting in macro merge



kdauria
04-21-2016, 02:18 PM
Hello,
I am in the process of creating a merge system for my department. I've worked up my base data file, merge documents, etc.
I have a macro to combine all my templates into one word doc while preserving font both in the doc and from the merge, and footers, and also adds the appropriate section breaks. Only thing is that when the files get pulled in, it seems that text anywhere I have tabbed in the original has been pushed over in the document. Is there something I can add to my existing macro code to preserve all elements of the documents.
This is what I'm working with:


Sub newnew()


Dim rng As Range
Dim MainDoc As Document
Dim strFile As String, strFolder As String
Dim Count As Long
With Application.FileDialog(msoFileDialogFolderPicker)
.Title = "Pick folder"
.AllowMultiSelect = False
If .Show Then
strFolder = .SelectedItems(1) & Application.PathSeparator
Else
Exit Sub
End If
End With
Set MainDoc = Documents.Add
strFile = Dir$(strFolder & "*.dot") ' can change to .docx
Count = 0
Do Until strFile = ""
Count = Count + 1
Set rng = MainDoc.Range
With rng
.Collapse 0
If Count > 1 Then
.InsertBreak 2
.End = MainDoc.Range.End
.Collapse 0
End If
.InsertFile strFolder & strFile
End With
strFile = Dir$()
Loop
MsgBox ("Files are merged")
lbl_Exit:
Exit Sub


End Sub

Thanks!