Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations biv343 on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Printing Word Mail-merged letters from VFP6

Status
Not open for further replies.

RichardH

Programmer
Apr 21, 2001
46
US
I want to print Word 2000 mail-merged letters for my VFP application. For example I have created a document in Word “Myletter.doc” and bookmarked the fields “contact” and “company”. Then in the application I have used this code:

USE customers
public oWord
scan
oWord=CREATEOBJECT("Word.Basic")
oWord.FileOpen ("myletter.doc")
oWord. ww7_EditGoto ("contact")
oWord.Insert (customers.Contact)
oWord. ww7_EditGoTo ("company")
oWord.Insert (customers.Company)
oWord.FilePrint
wait window 'Printing please wait...' timeout(1)
oWord.FileSaveAs("tempword.doc")
oWord.AppClose()
endscan
release oWord

It seem to work ok, but I wonder if there is something better. It seems that I shouldn’t have to create and new instance of Word every time I step to the next record, but if I don’t I get ole errors. The “wait” command seems useless, but without it I get a message that I shouldn’t close word when it is trying to print. And the FileSaveAs command also seems unnecessary but required to keep my original document in tact.

Is there some better code I should use?

Thanks…Rick
 
Try creating a word merge template (and be sure to mark it as read only, so it can't be accidently changed). In your code, create a text file that has as a first line the names of the merge fields, then, loop through writing each piece of data. then you can merge the two, diplay, print or print to file as nescessary.

To preview the two files:
oWord = CREATEOBJECT("oleWordMerge")
oWord.lShowWord = .T.
&& nDestination: 0 = send to new document, send to printer = 1
oWord.nDestination = 0
&& nDocType: 1 = mailing labels
oWord.nDocType = 1
oWord.cWordDoc = "c:\mydocs\mymergetemplate.doc"
oWord.cDataSource = "c:\mydocs\textfile.txt"
&& if the merged file is to be saved, otherwise set cNewDocumnet to ""
oWord.cNewDocument = "c:\mydocs\savedmerge.doc"
oWord.Merge()

In the text file the first line should something like:
"INTERNALRECORDNUMBER","CONTACT","COMPANY"
the first record would be:
"1","Sam Smith","Microsoft Corporation"
and the second:
"2","Jane Doe", "IBM"
with each record separated by a crariage return/line feed

I hope this helps!
 
On the other hand, if you want something close to what you posted, try:

Code:
 USE customers
public oWord
oWord=CREATEOBJECT("Word.Basic")
scan 
oWord.FileOpen ("MyLetter.doc")
oWord.EditGoto("Contact")
oWord.Insert(customers.Contact)
oWord.EditGoTo("Company")
oWord.Insert(customers.Company)
oWord.FilePrint
wait window 'Printing please wait...' timeout(3) 
oWord.Fileclose(2)
endscan
oWord.AppClose()
release oWord
I'm not sure why you needed the 'ww7' in your code, it didn't work for me using Word for Windows 95 (running on Windows 98). Fileclose (2) closes without saving, so you don't mess up your original template. --Dave
 
Dave,

Thanks for the code. I like your idea as it makes no sense to me to convert perfectly good VFP data to ASCII text before sending it to Word.

The "ww7" stuff I got from the Microsoft knowledge base and my program seems to run fine with it removed. However I do get an error when it loops to the line:

oWord.FileOpen ("MyLetter.doc")

for the second record. It prints the first letter fine but then I get the error:

"old lDispatch exception code 0 from Microsoft Word. This file could not be found...."

I am using VFP6 and Word 2000. Any ideas?

Thanks...Rick
 
Does anyone know why the code that Dave suggested gives the error I described above?

Sorry for the double post but the Discussion group just gave me an error when I submitted the last post.

Thanks...Rick
 
I think the problem is that the myletter.doc isn't in the same directory as where the program is running. Maybe .fileprint changes the working directory after the first run. Try running just one letter and then doing cd in the command window to see if you're where you should be.
If not, add whatever pathing you need.

Dave
 
Dave,

Thanks again for the help. You were right about the directory. The file myletter.doc is in c:\myapp folder where the program is running and if I type CD if reveals that I am still in c:\myapp after the first letter, but for some reason Word is lost.

However changing the line to:

oWord.FileOpen ("c:\myapp\MyLetter.doc")

fixes the problem.

Thanks...Rick
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top