Re: Merging two documents by Peter
Peter
Tue Dec 07 12:13:46 CST 2004
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
=20
' open the titles document in the background
Set src =3D Application.Documents.Open(FileName:=3D"MovieTitles.doc", =
Visible:=3DFalse)
=20
' initialize a new collection to hold titles
Set titles =3D 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 =3D True
.Font.Color =3D 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)
=20
' open the info doc in the background
Set src =3D Application.Documents.Open(FileName:=3D"MovieInfo.doc", =
Visible:=3DFalse)
=20
' open the destination document in the foreground
Set dest =3D Application.Documents.Add(Visible:=3DTrue)
=20
' 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)
=20
' make sure the dest doc is in the foreground
Call dest.Activate
=20
' cleanup
Set titles =3D Nothing
=20
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
"Malik Al-Amin" <malamin2004@yahoo.com> wrote in message =
news:Ol$ezLH3EHA.480@TK2MSFTNGP10.phx.gbl...
> 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.
>=20
> MovieInfo contains movie information such as:
> Godfather
> A cinematic classic depicting the lives of ....
>=20
> 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.
>=20
> Malik