Please Note: This article is written for users of the following Microsoft Word versions: 2007, 2010, 2013, 2016, 2019, Word in Microsoft 365, and 2021. If you are using an earlier version (Word 2003 or earlier), this tip may not work for you. For a version of this tip written specifically for earlier versions of Word, click here: Combining Documents.

Combining Documents

Written by Allen Wyatt (last updated July 23, 2024)
This tip applies to Word 2007, 2010, 2013, 2016, 2019, Word in Microsoft 365, and 2021


5

Stuart wrote about a need he had of consolidating many different text files into a single Word document. (Note that Stuart wants to combine text documents into a Word document, not to combine multiple Word documents.) Inserting the files manually proved to be tedious, particularly when there were 20 or 30 different text files to be combined.

Fortunately for Stuart, his file names were predictable: C1000.TXT, C1001.TXT, etc. This makes putting together a macro to do the consolidation rather easy. The following example will look for files C1000.TXT through C1030.TXT, all in the C:\ directory, and combine them into the current document:

Sub CombineFiles()
    Dim J As Integer
    Dim sFile As String
    For J = 1000 To 1030
        sFile = "c:\c" & Trim(Str(J)) & ".txt"
        If (Dir(sFile) > "") Then
            Selection.InsertFile FileName:=sFile, ConfirmConversions:=False
            Selection.TypeParagraph
        End If
    Next
End Sub

If you want to change the range of files being inserted, just change the values at the beginning of the For ... Next loop. If the files are in a different directory, you can change the path used in the next code line, where sFile is set. If a file within the range is missing, it is automatically skipped.

There is an even easier way of combining files, however, that doesn't even involve the use of Word. You can use the following command at a Windows command prompt:

copy C1???.txt single.txt

This would combine up to 1000 files, C1000.TXT through C1999.TXT, into a single text file called SINGLE.TXT. The original files remain untouched. If you wanted to combine a smaller number of files, you could use this format:

copy C1020.txt+C1021.txt+C1022.txt+C1023.txt single.txt

This usage results in the four files, C1020.TXT through C1023.TXT, being combined into SINGLE.TXT. You can add as many files together in this manner as you desire.

There is one caveat to this technique, however. The copy command results in no extra characters being added to a file at all. In other words, the contents of C1021.TXT are placed immediately after C1020.TXT. For example if C1020.TXT contains "text 1020" and C1021.TXT contains "text 1021" then SINGLE.TXT will contain "text 1020text 1021". The only way around this to either edit each source file to make sure it ends with a carriage return, or to use the macro previously presented.

Note:

If you would like to know how to use the macros described on this page (or on any other page on the WordTips sites), I've prepared a special page that includes helpful information. Click here to open that special page in a new browser tab.

WordTips is your source for cost-effective Microsoft Word training. (Microsoft Word is the most popular word processing software in the world.) This tip (9807) applies to Microsoft Word 2007, 2010, 2013, 2016, 2019, Word in Microsoft 365, and 2021. You can find a version of this tip for the older menu interface of Word here: Combining Documents.

Author Bio

Allen Wyatt

With more than 50 non-fiction books and numerous magazine articles to his credit, Allen Wyatt is an internationally recognized author. He is president of Sharon Parq Associates, a computer and publishing services company. ...

MORE FROM ALLEN

Spell Check Misses Misspelled Words

If you do a spelling check and notice that Word doesn't catch a word that you know is misspelled, it is easy to get ...

Discover More

Selectively Importing Records

Want to easily control which records get imported from a text file into Excel? It's easy to do when you write the macro ...

Discover More

Dealing with Small Time Values

It is no secret that you can store time values in an Excel worksheet. But do you really know how small of a time value ...

Discover More

Do More in Less Time! Are you ready to harness the full power of Word 2013 to create professional documents? In this comprehensive guide you'll learn the skills and techniques for efficiently building the documents you need for your professional and your personal life. Check out Word 2013 In Depth today!

More WordTips (ribbon)

Setting the AutoRecover Directory

When you are using Word, it normally saves temporary AutoRecover files that reflect the latest state of your document. If ...

Discover More

Determining the Length of a Non-Document Text File

If you use a macro to create and work with text files, you can find out the length of those files using a simple command. ...

Discover More

Setting Your Default Document Directory

Word allows you to specify where it should start looking for your documents. This setting can come in handy if you store ...

Discover More
Subscribe

FREE SERVICE: Get tips like this every week in WordTips, a free productivity newsletter. Enter your address and click "Subscribe."

View most recent newsletter.

Comments

If you would like to add an image to your comment (not an avatar, but an image to help in making the point of your comment), include the characters [{fig}] (all 7 characters, in the sequence shown) in your comment text. You’ll be prompted to upload your image when you submit the comment. Maximum image size is 6Mpixels. Images larger than 600px wide or 1000px tall will be reduced. Up to three images may be included in a comment. All images are subject to review. Commenting privileges may be curtailed if inappropriate images are posted.

What is 6 - 0?

2024-05-17 10:30:16

Timothy J. McGowan

I know this is only about four years late, but for anyone else with the same question as Warren, the Dir function is described here:
https://learn.microsoft.com/en-us/office/vba/language/reference/user-interface-help/dir-function

The line of code in full:
If (Dir(sFile) > "") Then

The Dir function will return "" if there is no such file on the system; it's an empty string, signifying that nothing is there. That's how the macro ignores any missing file in the file-name sequence.

Thus, the line of code means, as long as the number of characters in variable sFile is greater than "" (practically speaking, as long as there's a valid file specified in sFile), then continue to the following line; otherwise, drop to the Next statement and continue the loop.


2020-07-28 13:04:02

Warren Caragata

Hi

I am not sure about this line in the macro:

(Dir(sFile) > "")

Why this syntax and why brackets?


Thanks, Warren


2020-03-23 08:15:50

Bruno Quadros Rodrigues

When it combines the files, does it also update the page numbers?


2017-01-08 10:30:13

Thomas Redd

Enora,
I have the same history with Word Perfect. I am using Word 2016 and it seems to work okay for all my master documents now. I have some master documents that expand to more than 2000 pages and I have had no problems lately. I did struggle before with Word. I think you will have it work well for you.


2017-01-07 19:02:14

Enora

Back in WordPerfect days, I used a master document procedure that worked very well. (Set up a master document, sub documents, and on command, WordPerfect combined them all. The beauty of the system was the ability to edit individual documents, split them apart and then recombine as needed.) Word had an equivalent process but it was fraught with problems and could not be trusted. With each new Word version, I tested and read comments and never felt safe using it. Are things better now? I'm currently using 2010 and have NOT looked into it.


This Site

Got a version of Word that uses the ribbon interface (Word 2007 or later)? This site is for you! If you use an earlier version of Word, visit our WordTips site focusing on the menu interface.

Videos
Subscribe

FREE SERVICE: Get tips like this every week in WordTips, a free productivity newsletter. Enter your address and click "Subscribe."

(Your e-mail address is not shared with anyone, ever.)

View the most recent newsletter.