Welcome toWord.Tips.Net
Ask a Word Question
Make a Comment
Learn Access Now
Free Printable Forms
Beauty Tips
Car Tips
Cleaning Tips
College Tips
Cooking Tips
Excel2007 Tips
ExcelTips
Family Tips
Gardening Tips
Health Tips
Home Tips
Legal Tips
Money Tips
Organizing Tips
Pest Tips
Pet Tips
Wedding Tips
Word2007 Tips
WordTips
Printing On Both Sides of the Paper
Turning Off AutoComplete for Dates
Understanding Auto Line Spacing
Adding Comments to Your Document
Conditional Calculations in Word
Stuart wrote about a need he had of consolidating many different text files into a single Word document. Using the Insert | File option 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" & 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 the DOS command line:
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.
Tip #1607 applies to Microsoft Word versions: 6 95 97 2000 2002 2003
Create and Merge! Using Word's mail merge tool you can quickly and easily combine data from a variety of data sources to create great individualized documents that incorporate your data in ways that you control. WordTips: Mail Merge Magic is an invaluable source for learning how to harness the full power of Word's mail merging capabilities.