Word Copy Multiple Documents into One | VBA Macro #21

Share on facebook
Share on twitter
Share on linkedin

Macro Intro

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 Word Copy Multiple Documents into One 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!

See it in Action

Watch this video to see this macro in action.

Code

Here is the code for this macro. Make sure the following References are setup before running it: Visual Basic For Applications, Microsoft Office 16.0 Object Library, Microsoft Word 16.0 Object Library
***Recent Code Updates
MM.DD.YYYY Recent Code Update Description.
Copy the Code
'Leverage & Lean "Less Clicks, More Results"
Sub CopyMultipleDocumentsintoOneFreeMacro()
' Means variable is in use
Dim ActiveDoc As String '
Dim FolderPath As String '
Dim oFile As Object '
Dim oFolder As Object '
Dim oFSO As Object '
  
On Error GoTo LeverageLean
 
ActiveDoc = ActiveDocument.Name 'Get the Active Word Document's Name (Referenced Later On)
 
'Selection.HomeKey wdStory 'Move to Start of the Active Document (this is where additional content will be pasted)
Selection.EndKey wdStory 'Move to End of the Active Document(this is where additional content will be pasted)
  
FolderPath = Left(ActiveDocument.FullName, Len(ActiveDocument.FullName) - Len(ActiveDocument.Name)) 'Active Word Document's File Path
 
Set oFSO = CreateObject("Scripting.FileSystemObject")
Set oFolder = oFSO.GetFolder(FolderPath) 'Set the Folder to loop through for Word Documents
  
For Each oFile In oFolder.Files 'Loop through every File in FolderPath
If InStr(1, oFile.Name, ".doc") <> 0 And oFile.Name <> ActiveDoc And InStr(1, oFile.Name, "~") = 0 Then 'If the File Type contains the File Extension .doc and is NOT Active Document and is NOT Lock File
Application.Documents.Open FolderPath & "\" & oFile.Name, Visible:=False 'Open the Word Document found and don't make it Visible
Documents(oFile.Name).Activate 'Activated Open Word Document
Selection.WholeStory 'Select the entire Word Document
Selection.Expand wdParagraph 'Expands your selection to th end of the Word Document
Selection.Copy 'Copy the content in the Word Document
Documents(oFile.Name).Close 'Close Word Document
Documents(ActiveDoc).Activate 'Activate the original Word Document
Selection.InsertAfter ("***Content from " & Left(oFile.Name, InStr(1, oFile.Name, ".") - 1) & ":" & vbNewLine) 'Indicate where the content is from and insert it into the original Word Document
Selection.Collapse Direction:=wdCollapseEnd 'Add a Paragraph following the Content From open Word Document
Selection.PasteAndFormat wdPasteDefault 'Paste Word Document content into the original Word Document
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

Insider Content

Here is the Insider code for this macro. This code will allow you to setup an Excel Workbook that can hold a list of Word Documents to combine, even if they are saved to different folders! You can also listed multiple folders that can be looped through looking for Word Documents to combine! Make sure the following References are setup before running it: Visual Basic For Applications, Microsoft Office 16.0 Object Library, Microsoft Word 16.0 Object Library, Microsoft Excel 16.0 Object Library

Customization

These segments of code can be customized to personalize this macro.
The Selection.HomeKey wdStory and Selection.EndKey wdStory code lines are used to determine where the additional content captured from the other Word Documents will be pasted. By Default, Selection.EndKey wdStory is used to paste additional content at the end of the Active Word Document. If you want the additional content to paste at the beginning of the Active Word Document remove the comment from Selection.HomeKey wdStory and make a comment or delete the Selection.EndKey wdStory. (Learn more about the Selection.HomeKey and Selection.EndKey method)
By Default, the FolderPath variable is set to the Active Word Document folder path to loop through the existing files and find other Word Documents to pull content from. If you want to loop through a folder that is different from the Active Word Document you can update the FolderPath variable.
This code line is used to filter out what files are appropriate to open in the folder determined from the FolderPath variable. If the File Name contains .doc in the File Extension AND the File Name is NOT the Active Word Document AND the File Name does NOT contain the ‘~’ symbol meaning it is the Active Word Document’s Lock File. If you want to add additional criteria to exclude certain file you can do so here. (Keep in mind you can always move files out of the folder or update the folder path completely.)
To help determine where the additional content originates from insert the text ***Content from [Word Document Name] before the pasted content. This will also help separate the content of multiple Word Documents from each other. Learn more about the InsertAfter method here! If you don’t want any separation of the additional content you can remove this code line or make it a comment.
This code line will remove the selection of the Content From text section and insert a NEW Paragraph. This helps ensure pasting in the additional Word Document content doesn’t overwrite where it originates from. Learn more about the Collapse method here!
When pasting in the additional content from the other Word Document(s) use the PasteAndFormat Method and the wdPasteDefault parameter. This will allow for the pasting of things like images, shapes, tables, etc. See more pasting types that can be used in the PasteAndFormat method here!
Display a MsgBox at the end of this macro to confirm that Multiple Word Documents have been copied into this one! If you don’t want this behavior remove this code line or make it a comment.

First time using VBA?

The Developer Tab is an additional section of the ribbon when activated allows you access to Visual Basic in Applications like Access, Excel, Outlook, PowerPoint, Word.
Setup Now
Once the Developer Tab is available you will have the capability to update your security to allow Macros to run in the current application.
Setup Now
A sub of code is a collection of objects and variables. For the code to successfully run a library of references needs to be set.
Setup Now
Subscribe as an Insider to receive additional rights.
If you like our content and want to show your support tip us here!

Contact Us

Looking to improve your computer processes?
Leverage & Lean is here to help!

Powered By MemberPress WooCommerce Plus Integration

Free Macro

Start creating Less Clicks, More Results today!