How to use OLE to combine word doci=uments and create one word doc

How to use OLE to combine word doci=uments and create one word doc

Post by dhab » Wed, 13 Dec 2006 00:07:35


I have a 43 .doc files. i would like to combine all files in one.doc
file? can i use fox pro OLE to accomplish above
 
 
 

How to use OLE to combine word doci=uments and create one word doc

Post by Cindy Wine » Wed, 13 Dec 2006 05:04:49

Hi Dhabu,

Yes, you can do this. Your basic plan should be to instantiate Word and
create a new document. Then get a list of the files you want to copy from
(use ADir() ?), open each one, select all the text, and then paste into the
new document. I'm sure you'll want to insert some sort of dividers between
one document and the next. The confusing part is navigating the Word object
model. Your code would go something like this:

*-- aDocs is an array with the paths and doc names

*-- Open Word
oWord = CreateObject("Word.Application")
oWord.Visible = .T. && For testing

*-- Open a blank document to be your compiled document
oDocOut = oWord.Documents.Add() && The compiled document

*-- Loop through your list of docs to copy
For lnCount = 1 to ALen(aDocs)

*-- Open a document and get the text
oDocIn = oWord.Documents.Add(aDocs(x))
_ClipText = oDocIn.StoryRanges(1).Text

*-- Go to your compiled document and insert the text
*-- Note that you want to use StoryRanges(1) all the way through
oDocOut.StoryRanges(1).InsertAfter(_ClipText)
*-- Insert some divider text here

EndFor



--
Cindy Winegarden MCSD, Microsoft Most Valuable Professional
XXXX@XXXXX.COM

 
 
 

How to use OLE to combine word doci=uments and create one word doc

Post by Roger Anse » Wed, 13 Dec 2006 12:45:43


Same sort of approach as Cindy's but without the need
to copy/paste ...

Local loWord As "Word.Application"
Local loMainDoc As "Word.Document"
Local lnDocs As Integer, i As Integer
#Define wdPageBreak 7

loWord = CreateObject("Word.Application")

lnDocs = ADir(laDocs,"*.doc")

loMainDoc = loWord.Documents.Add()

For i = 1 to lnDocs
With loMainDoc.ActiveWindow.Selection
.InsertFile(laDocs(i,1))
.InsertBreak(wdPageBreak) && optional
EndWith
Next

loWord.Visible = .T.