VBA to merge all files in folder not merging in correct order.

Akbarov

Active Member
Joined
Jun 30, 2018
Messages
347
Office Version
  1. 365
Platform
  1. Windows
Hello Dear community,

I use following code to merge all Word documents in specific folder. "Generate"
All file names in specified folder are named as: Part1,Part2,Part3...
But it is merging with wrong order as : Part1,Part10,Part11 etc..
Can anyone help me to make it merge all with correct order please?

Thanks in advance,

VBA Code:
Sub merge()
  Dim dlgFile As FileDialog
  Dim objDoc As Document, objNewDoc As Document
  Dim StrFolder, strFile As String
 
  Set dlgFile = Application.FileDialog(msoFileDialogFolderPicker)
 
 myPath = ThisDocument.Path & "\Generate\"
 
  strFile = Dir(myPath & "*.docx", vbNormal)
  Set objNewDoc = Documents.Add
 
  While strFile <> ""
    Set objDoc = Documents.Open(FileName:=myPath & strFile)
    objDoc.Range.Copy
    objNewDoc.Activate
    With Selection
      .Paste
      '.InsertBreak Type:=wdPageBreak
      .Collapse wdCollapseEnd
    End With
 
    objDoc.Close SaveChanges:=wdDoNotSaveChanges
 
    strFile = Dir()
  Wend
 
  objNewDoc.Activate
  Selection.EndKey Unit:=wdStory
  Selection.Delete
  objNewDoc.SaveAs myPath2
End Sub
 

Excel Facts

Control Word Wrap
Press Alt+Enter to move to a new row in a cell. Lets you control where the words wrap.
All file names in specified folder are named as: Part1,Part2,Part3...

Untested, replace the While ... Wend loop with this:
VBA Code:
  Dim n As Long
  n = 1
  While Dir(myPath & "Part" & n & ".docx") <> vbNullString
    Set objDoc = Documents.Open(FileName:=myPath & "Part" & n & ".docx")
    objDoc.Range.Copy
    objNewDoc.Activate
    With Selection
      .Paste
      '.InsertBreak Type:=wdPageBreak
      .Collapse wdCollapseEnd
    End With
    objDoc.Close SaveChanges:=wdDoNotSaveChanges
    n = n + 1
  Wend
 
Upvote 0
Solution
Thank you so much John! Seems like everything working perfectly fine.
 
Upvote 0

Forum statistics

Threads
1,223,249
Messages
6,171,031
Members
452,374
Latest member
keccles

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top