Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
Home
DiscussionsAccessExcelInfoPathOutlookPowerPointPublisherWord
DirectoryUser Groups
Related Topics
Outlook ExpressInternet ExplorerWindowsMS Server ProductsMore Topics ...

MS Office Forum / Word / Programming / December 2004

Tip: Looking for answers? Try searching our database.

Merging two documents

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
Malik Al-Amin - 07 Dec 2004 15:39 GMT
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
Peter - 07 Dec 2004 18:13 GMT
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:úlse)
 
 ' 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:úlse)
 
 ' 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

> A friend of mines asked me to help him with this project. We have two
> separate word documents. MovieTitles.doc and MovieInfo.doc
[quoted text clipped - 18 lines]
>
> Malik
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.