Skip to content

October 3, 2011

Combine several word documents into one [tested on word 2007]

I cam across a scenario where I needed to combine many word files (50+) into one be document and still keep the original files so they could be edited.  I discovered word had a feature where you could include text from another file, which was exactly what I wanted but you could only do it one file at a time…this would take too much time to do it manually, time for a script.

Below is the VBscript I wrote to do this.  Add this to your normal file so that it is available on every instance of word which you run, you can even add a icon to the quick menu for easy access.

 

Sub InsertText()
Dim doc As Word.Document
Dim bkmName As String
Dim SigFile As String
Dim fd As FileDialog
Set fd = Application.FileDialog(msoFileDialogFilePicker)
Set doc = ActiveDocument
Dim vrtSelectedItem As Variant
Dim theText() As String
Dim theText2 As StringWith fd
.Filters.Add “Docs”, “*.doc; *.docx”, 1

‘Sets the initial file filter to number 2.
.FilterIndex = 1

If .Show = -1 Then

For Each vrtSelectedItem In .SelectedItems
Set mg2 = ActiveDocument.Range
mg2.Collapse wdCollapseEnd
theText2 = vrtSelectedItem

doc.Fields.Add Range:=mg2, Type:=wdFieldEmpty, Text:= _
“INCLUDETEXT “”" & replaceSlashes(theText2) & “”" “, PreserveFormatting:=True

Set mg1 = ActiveDocument.Range
mg1.Collapse wdCollapseEnd
‘mg1.MoveEnd Unit:=wdCharacter, Count:=-1

mg1.InsertBreak wdPageBreak

Next vrtSelectedItem
Else
End If
End With

‘Set the object variable to Nothing.
Set fd = Nothing

End Sub

Function replaceSlashes(sText As String)

sText = Replace(sText, “\”, “\\”)

replaceSlashes = sText

End Function

Read more from Misc

Share your thoughts, post a comment.

(required)
(required)

Note: HTML is allowed. Your email address will never be published.

Subscribe to comments

*

This blog is monetized using Are-PayPal WP Plugin