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)

RE: VFP9 Word 2003 merge problem by BernieBeattie

BernieBeattie
Wed Aug 27 05:20:02 CDT 2008

I may as well reply to my own post in case someone else hits this problem.

It is really bizarre. The code works perfectly if you step through it in
debug mode. To fix it, I forced the code to pause (wait window "" timeout 1)
after setting the word activeprinter and before calling the printout method.
Now works fine. Can't explain why.

Bernie

"Bernie Beattie" wrote:

> 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)
>