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 Outlook Application

Outlook Greeting and Goodbye Contacts | VBA Macro #35

The Greeting and Goodbye Contact macro automates the entry of a Greeting “Good Morning” or “Hello” followed by the First Name of the recipient you are emailing. To identify the First Name this macro uses the recipient’s email address to loop through all the existing contact records for a matching email address. If a match is found the contact’s First Name field value is used in the beginning of the email. The appropriate Greeting and Goodbye is determined by the current time and the day of the week. The Greeting and Goodbye Contacts macro helps you deliver better customer service and create better connections with your clients!

VBA Macro used in the Microsoft Office Excel Application

Excel Custom Questionnaire to File | VBA Macro #34

This is an Excel macro that generates a custom questionnaire to complete.  The Role field is used to determine what questions display for specific users.  Once a user selects their Role, they can click Generate to see what questions they need to complete to finish the questionnaire.  After a user has completed all the required questions, they can click Finish to create a separate Excel Workbook with all the populated answers.   You can customize where this new file is saved making it easy to know when someone has completed a questionnaire and is awaiting review. There is also the option to send this separate Excel Workbook in an Outlook email with a couple updates to the VBA code. There is always a better way with VBA and this Excel macro helps standardize the collection of answers to specific questions. This can help lead to a more formal review process and eliminate mistakes.

VBA Macro used in the Microsoft Office Outlook Application

Outlook Hello Contacts | VBA Macro #33

Starting an email with a Hello can look more personable with some VBA code! The Hello Contacts Outlook macro automates the entry of the text Hello followed by the First Name of the recipient you are emailing. This Outlook macro uses the recipient’s email address to loop through all the existing contact records for a matching email address. If a match is found the contact’s First Name field value is used in the beginning of the email. This macro can be fired from a custom button or trigger automatically when replying to an email.

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