Have you ever had different Word Documents coming from various sources and need to combine them into one? Say for example a different team member is assigned to work on the Beginning, Middle, and End of a paper. Once each part is complete you have to open each Word Document, select all the content, then copy and navigate back to your original document to paste. Fortunately, there is a better way with VBA! With the Copy Multiple Documents into One macro you can quickly combine all these Word Documents with one click! This Word macro loops through all the existing Documents found at the Folder Path. By Default, the Folder Path is the location of the Active Word Document. Each Document found is opened, copied, and pasted into the original Word Document. By Default, the content will be pasted at the End of the Document. (You can customize this and alternatively paste the Beginning) Before pasting additional text is entered to indicate where the content originated from. This helps you know if the content needs to be moved because it is out of order. Prior to running this macro, you can rename the Word Documents in a way that the order is correct instead of having to move the content after the fact. The Copy Multiple Documents into One macro Word macro allows you to delegate more work because you can efficiently combine all the efforts of your team. If you like this concept checkout our Excel macro that Copies Multiple Workbooks into One!
15. ‘Selection.HomeKey wdStory ‘Move to Start of the Master Document If you want the copied content to be pasted to the Beginning of the Master Document remove the comment in code line 15 and make code line 16 a comment
16. Selection.EndKey wdStory ‘Move to End of the Master Document By Default the copied content will be pasted to the End of the Master Document
18. FolderPath = Left(ActiveDocument.FullName, Len(ActiveDocument.FullName) – Len(ActiveDocument.Name)) ‘Active Document File Path By Default the Folder Path is the Active Word Document but you can change this to a different path if desired
35. Selection.InsertAfter (“***Content from ” & Left(oFile.Name, InStr(1, oFile.Name, “.”) – 1) & “:” & vbNewLine) ‘Source of the content Remove or make this code line a comment to NOT get the indication of where the content was copied from
41. MsgBox “Multiple Word Documents have been copied into this one!” Remove the MsgBox or make it comment to not see it once the macro finishes
See it in Action!
Watch this video to see this macro in action.The Code
Here is the code for this macro. Make sure the following References are setup before running it: Visual Basic For Applications, Microsoft Word 16.0 Object Library'Leverage & Lean "Less Clicks, More Results" Sub CopyMultipleDocumentsintoOne() ' Means variable is in use Dim FolderPath As String ' Dim MasterDoc As String ' Dim oFile As Object ' Dim oFolder As Object ' Dim oFSO As Object ' Dim SeparateDoc As String ' On Error GoTo LeverageLean MasterDoc = ActiveDocument.Name 'Selection.HomeKey wdStory 'Move to Start of the Master Document Selection.EndKey wdStory 'Move to End of the Master Document FolderPath = Left(ActiveDocument.FullName, Len(ActiveDocument.FullName) - Len(ActiveDocument.Name)) 'Active Document File Path Set oFSO = CreateObject("Scripting.FileSystemObject") Set oFolder = oFSO.GetFolder(FolderPath) For Each oFile In oFolder.Files 'Loop through every File in FolderPath If InStr(1, oFile.Type, "Microsoft Word") <> 0 And oFile.Name <> MasterDoc And InStr(1, oFile.Name, "~") = 0 Then 'If the File Type contains the phrase Microsoft Word and is NOT Active Document and is NOT Lock File Application.ScreenUpdating = False 'Hide Screen Updates Application.DisplayAlerts = False 'Hide Display Alerts Application.Documents.Open FolderPath & "\" & oFile.Name, Visible:=False Documents(oFile.Name).Activate 'Open Separate Document SeparateDoc = ActiveDocument.Name Selection.WholeStory 'Select whole document Selection.Expand wdParagraph 'Expands your selection to current paragraph Selection.Copy 'Copy the content in the Separate Document Documents(oFile.Name).Close 'Close Separate Document Documents(MasterDoc).Activate 'Activate Master Document Selection.InsertAfter ("***Content from " & Left(oFile.Name, InStr(1, oFile.Name, ".") - 1) & ":" & vbNewLine) 'Source of the content Selection.Collapse Direction:=wdCollapseEnd Selection.PasteAndFormat wdPasteDefault 'Paste Separate Document Content into Master End If Next oFile MsgBox "Multiple Word Documents have been copied into this one!" Set oFSO = Nothing Set oFolder = Nothing Set oFile = Nothing Exit Sub LeverageLean: MsgBox (Err.Number & " - " & Err.Description & vbNewLine & vbNewLine & "Don't hesitate to email me: brentschneider@leveragelean.com") End Sub 'Stay Awesome
Macro Monday
Here is the Macro Monday video this macro was featured in. Watch this video to learn how to get the most out of this macro and start using it today!Customization
These lines of code can be customized to personalize this macro.15. ‘Selection.HomeKey wdStory ‘Move to Start of the Master Document If you want the copied content to be pasted to the Beginning of the Master Document remove the comment in code line 15 and make code line 16 a comment
16. Selection.EndKey wdStory ‘Move to End of the Master Document By Default the copied content will be pasted to the End of the Master Document
18. FolderPath = Left(ActiveDocument.FullName, Len(ActiveDocument.FullName) – Len(ActiveDocument.Name)) ‘Active Document File Path By Default the Folder Path is the Active Word Document but you can change this to a different path if desired
35. Selection.InsertAfter (“***Content from ” & Left(oFile.Name, InStr(1, oFile.Name, “.”) – 1) & “:” & vbNewLine) ‘Source of the content Remove or make this code line a comment to NOT get the indication of where the content was copied from
41. MsgBox “Multiple Word Documents have been copied into this one!” Remove the MsgBox or make it comment to not see it once the macro finishes