Created
October 10, 2020 10:01
-
-
Save ap-Codkelden/a8ff9bfae913f3d3092e874f96ec5784 to your computer and use it in GitHub Desktop.
Merge multiple doc(x) into one
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sub MergeDocs() | |
On Error GoTo ErrorHandler | |
Dim rng As Range | |
Dim MainDoc As Document | |
Dim strFile As String, strFolder As String | |
Dim Count As Long | |
With Application.FileDialog(msoFileDialogFolderPicker) | |
.Title = "Selectos" | |
.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*") ' can change to .docx | |
Count = 0 | |
Do Until strFile = "" | |
Count = Count + 1 | |
Set rng = MainDoc.Range | |
With rng | |
.Collapse wdCollapseEnd | |
If Count > 1 Then | |
.InsertBreak wdPageBreak ' wdSectionBreakNextPage | |
.End = MainDoc.Range.End | |
.Collapse wdCollapseEnd | |
End If | |
.InsertFile strFolder & strFile | |
End With | |
strFile = Dir$() | |
Loop | |
MsgBox ("Files are merged") | |
lbl_Exit: | |
Exit Sub | |
ErrorHandler: | |
MsgBox (strFile) | |
Resume Next | |
End Sub |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment