VBA Macro used in the Microsoft Office Word Application

Word Copy Multiple Documents into One | VBA Macro #21

Share on facebook
Share on twitter
Share on linkedin
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!

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
Subscribe
Notify of
0 Comments
Inline Feedbacks
View all comments

Search for Word Macros

Recent Macros

VBA Macro used in the Microsoft Office Word Application

Word InputBox Call Macros | VBA Macro #27

More Macros More Buttons.  If you create enough macros you will begin to question if a button needs to be created for each one.  It is overwhelming when there are too many custom macro buttons to select from. This also makes navigating to run a specific macro difficult. To address this issue, we created the macro Word InputBox Call Macros! This Word macro displays an InputBox with a list of macros that can be called.  To call a specific macro just enter in the leading number and click ok. The concept of this macro is simple and helps you stay organized storing all macros within one button.

VBA Macro used in the Microsoft Office Excel Application

Excel Update Multiple Workbooks from One | VBA Macro #26

This Excel macro is the inverse of Copy Multiple Workbooks into One where separate Workbooks are being combined into one MASTER Workbook.  The Update Multiple Workbooks from One macro instead takes the current information from the MASTER Workbook and updates separate Workbooks. If you desire one source of information that can be easily distributed across multiple parties this macro is for you!

VBA Macro used in the Microsoft Office Outlook Application

Outlook Create Multiple Emails | VBA Macro #25

You just completed a task on your computer and now you need to email some impacted parties letting them know the task is complete.  Let’s say for example you have to email the client, the billing department, and your supervisor.  You could send one email including all these recipients but this is a problem if you are sending sensitive information specific to everyone involved.  Even if there isn’t sensitive information someone can use Reply All creating an email thread with everyone included. There has to be a better way…of course there is with VBA! This Outlook macro will create multiple emails based on the number that you indicate in an InputBox.  Think of this Create Multiple Emails macro as a code template that allows you generate multiple emails pertaining to different topics. Using the example above the first email can be dedicated to the client, the second email for the billing department, and the third email for your supervisor.

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 Excel, Outlook, 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

Looking for more?

Subscribe as an Insider to receive additional rights.

Contact Us

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

Copyright © 2019 Leverage & Lean – Uptown Style WordPress theme by GoDaddy