Merging two documents

M

Malik Al-Amin

A friend of mines asked me to help him with this project. We have two
separate word documents. MovieTitles.doc and MovieInfo.doc
Movietitles is just a document with movie titles formatted as follows:
Godfather
Speed
Matrix
etc.

MovieInfo contains movie information such as:
Godfather
A cinematic classic depicting the lives of ....

What we'd like to do is open both of these documents behind the scenes. If
we have matching titles we want to create a third document that includes the
movietitle from movietitles.doc and underneath the title we want to indent
and include all of the movieinfo from the movie info from the movieinfo.doc
We want the text bold and changed to the color blue. Finally we'd like to
save this as a third document. So far they've been doing this manually. in
my mind this process is screaming for automation. Any advise with this
process is welcome. Thanks in advance.

Malik
 
P

Peter

This should give you something to work with:

Sub Combine()
Dim src As Document
Dim dest As Document
Dim para As Paragraph
Dim titles As Collection

' open the titles document in the background
Set src = Application.Documents.Open(FileName:="MovieTitles.doc", Visible:=False)

' initialize a new collection to hold titles
Set titles = New Collection
' iterate through the paragraphs (lines) of the titles document
For Each para In src.Paragraphs
With para.Range
' apply the desired formatting
.Bold = True
.Font.Color = wdColorBlue
' add the formatted paragraph (line) to the collection, keying on the unformatted text
Call titles.Add(.FormattedText, .Text)
End With
Next para
' close the titles doc, discarding changes
Call src.Close(False)

' open the info doc in the background
Set src = Application.Documents.Open(FileName:="MovieInfo.doc", Visible:=False)

' open the destination document in the foreground
Set dest = Application.Documents.Add(Visible:=True)

' iterate through the paragraphs of the info doc
For Each para In src.Paragraphs
With para.Range
' see if there's a match with a title
If Not titles(.Text) Is Nothing Then
' if there is a match, insert the formatted title into the dest doc
Call dest.Range.InsertAfter(titles(.Text))
' followed by the next paragraph from the info doc, which should be the info
Call dest.Range.InsertAfter(para.Next.Range.FormattedText)
End If
End With
Next para
' close the info doc, discarding changes
Call src.Close(False)

' make sure the dest doc is in the foreground
Call dest.Activate

' cleanup
Set titles = Nothing

End Sub

It assumes that the layouts of the two documents are exactly as you described, and doesn't apply too much formatting.
You might be able to copy over para.Next instead of para.Next.Range.FormattedText, not sure.
I haven't tested this sub at all, but it should give you a starting point.
Let me know how it works.

hth,

-Peter
 

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