Word 2007 macro IncludeText

R

rkv

I have @400 individual Word files that I need to combine into a single Word
file. All the files live in one directory. I'm currently using an AutoOpen
macro to loop thru each of the individual files and 'include' the text using
"Selection.InsertFile".

This works ok but is very slow. Is there a better way to combine lots of
Word documents into a sigle word Document?
 
D

Doug Robbins - Word MVP

Dim MyPath As String
Dim MyName As String
Dim Source As Document, Target As Document
Dim SourceFile As Range
Dim i As Long
Dim FileList As Document
Set FileList = Documents.Add
'let user select a path
With Dialogs(wdDialogCopyFile)
If .Display() <> -1 Then Exit Sub
MyPath = .Directory
End With
'strip quotation marks from path
If Len(MyPath) = 0 Then Exit Sub
If Asc(MyPath) = 34 Then
MyPath = Mid$(MyPath, 2, Len(MyPath) - 2)
End If
'get files from the selected path
'and insert them into the doc
MyName = Dir$(MyPath & "*.*")
Do While MyName <> ""
Selection.InsertAfter MyName & vbCr
MyName = Dir
Loop
'Sort the list of files
FileList.Range.Sort SortFieldType:=wdSortFieldAlphanumeric, _
FieldNumber:="Paragraphs"
'Delete the empty paragraph that will be at the top of the list of files
FileList.Paragraphs(1).Range.Delete
'Start a new document into which each of the others will be inserted
Set Target = Documents.Add
'Iterate through the list of files, getting the name of each file, opening
it
'and inserting its contents into the Target document
For i = 1 To FileList.Paragraphs.Count
Set SourceFile = FileList.Paragraphs(i).Range
SourceFile.End = SourceFile.End - 1
Set Source = Documents.Open(MyPath & SourceFile.Text)
Target.Range.InsertAfter Source.Range.FormattedText
Source.Close wdDoNotSaveChanges
Next i

Also see the article "Print all documents in a given folder to a single
print file" at:

http://www.word.mvps.org/FAQs/MacrosVBA/PrintAllDocsInFldr.htm

--
Hope this helps.

Please reply to the newsgroup unless you wish to avail yourself of my
services on a paid consulting basis.

Doug Robbins - Word MVP
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top