I have some code that merges data into a Word merge document and prints it
automatically. It works great on pcs with Word 2000. However my client has
some pcs with Word 2003 and although the code runs through with no errors,
gobbledygook prints where the merge fields are supposed to be.
Any clues as to how I fix this? I'm going to try getting them to save
separate versions of the merge documents in Word 2003 but I wondered if
there's any other suggestions. Shoulf the automation code work no matter
what version of Word the user has? I've posted some sample code below.
Regards,
Bernie Beattie
*******Sample code**********
local loword, lcfile, lcdatafile, lodoc, lodocactive, llerror, lcprinter
* Get user's choice of printer
lcPrinter = GETPRINTER()
IF EMPTY(lcPrinter)
WAIT WINDOW "no printer"
RETURN .F.
ENDIF
SET PRINTER TO NAME (lcPrinter)
TRY
loWord = CREATEOBJECT("word.application")
CATCH
WAIT WINDOW "word not instantiated"
llerror=.t.
ENDTRY
IF NOT llerror
* Now try to open the document
lcFile = "c:\cast\test2.doc"
TRY
loDoc = loWord.Documents.Open(lcFile)
CATCH
WAIT WINDOW "file not opened"
llerror=.t.
ENDTRY
ENDIF
lcDatafile = "c:\cast\data.txt"
IF NOT llerror
TRY
* Attach the data source
loDoc.MailMerge.OpenDataSource(lcDataFile)
* Run the mailmerge
loDoc.Mailmerge.Destination = 0 && send mailmerge to a new doc
loDoc.Mailmerge.Execute
* The mailmerge doc is now the active doc
loDocactive = loWord.ActiveDocument
* Go ahead and print it
loWord.ActivePrinter = lcPrinter
loDocactive.Printout()
CATCH TO loError
WAIT WINDOW "file not printed"
ENDTRY
ENDIF
* Close both documents (without prompting to save changes)
loDoc.Close(0)
loDocactive.Close(0)