I am trying to merge many word documents into one word document using VBA Macros. I wrote the following code to do this task
Sub MergeDocs()
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 your 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 & "*.doc") '
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
This does it's job very well except the order of the docs is not in correct way.
So here is the example how it's working. If i merge the docs as doc1,doc2,doc3,doc4 into one doc, then this macro merges them all but in random order as doc3,doc2,doc4,doc1 where as i want the docs to be in order as doc1,doc2,doc3,doc4.
Could anyone please try it and help me to get this resolved as i am not having much exposure on VB Macros in MS-Word.
Appreciate your help.
Thanks