Level Extreme platform
Subscription
Corporate profile
Products & Services
Support
Legal
Français
Word throws exception after MailMerge
Message
From
29/10/2003 12:28:14
Cetin Basoz
Engineerica Inc.
Izmir, Turkey
 
 
To
29/10/2003 10:09:49
General information
Forum:
Visual FoxPro
Category:
COM/DCOM and OLE Automation
Miscellaneous
Thread ID:
00844080
Message ID:
00844153
Views:
20
>Hey all,
>
>I have the following code:
>
>
>DO CreateDSN IN odbc.prg WITH "TEMPVFP", pcDatabase, "DBC"
>lcConnectionString = [DSN=TEMPVFP;SourceDB=] + pcDatabase + [;SourceType=DBC]
>lcSQL = [SELECT * FROM ] + pcTable
>oWord = CREATEOBJECT("Word.Application")
>oDocument = oWord.Documents.Open(pcDocFile)
>WITH oDocument.MailMerge
>    .OpenDataSource(,,,.T.,,,,,,,,lcConnectionString,lcSQL,,,wdMergeSubTypeWord2000)
>    .Destination = wdSendToPrinter
>    .Execute()
>ENDWITH
>oWord.Visible = .T.
>oDocument.Close("0")    &&   <----- ERROR THROWN HERE
>oWord.Quit("0")
>oWord = .NULL.
>RELEASE oWord
>
>
>Basically, I am creating an ODBC DSN, hooking a Word MailMerge document to it, executing the merge straight to printer, and then wanting to close out.
>
>Everything works great except the closing out. On the line labeled (trying to close the document), I get an OLE error "The server threw an exception".
>
>I made Word visible to try to see if I could see anything weird. After the Close() errors out, Word is all hung up. You can see the menu and title, but the rest of the Word screen is frozen.
>
>If I manually go to Word when I make it visible and try to close the document (before throwing the error), I get a "Word has encountered an uxpected error" message, and Word bombs out, so my automation calls do not appear to be the problem.
>
>Finally, if I try to do a straight Quit, it also errors out and/or hangs. I need to figure out why the original doc won't close.
>
>I could rig a way to just kill all WINWORD processes, but I am afraid of killing machines by doing that, wasting memory until users are forced to reboot. What is going on here? Do I need to close my data connection somehow? If so, how is that done? Word works fine for everything else, including other merges that use DAT files. I can't use DAT files because my field list exceeds 255 characters, and if I cahnge my field names I will have to change a TON of other reports and imaging code that uses the existing data structure.
>
>Also, I can do the entire merge process just in Word through the merge wizard, same data and same doc, and it all works without hanging. It's got to be something with my OpenDataSource where Word is getting confused at me trying to dynamically set up its merge data. If there a better way to dynamically assign data to Word's merge? Please help!
>
>JoeK

Do not close or release word before it does printing. Search on mailmerge and you'd find code that waits for word to finish and closes it down properly (keywords might be BackGroundPrintingStatus and Mailmerge). ie:
Define Class myTimer As Timer
 Interval = 1000
 oWord = .null.
 
 Procedure init
 Lparameters toWord
 this.oWord = toWord
 endproc
 
  Procedure Timer
    With this.oWord
       If .BackgroundPrintingStatus > 0 && Wait printing to finish
        Return
      Endif
      For Each oDoc In .Documents
        oDoc.Close(wdDoNotSaveChanges)
      Endfor
      oDoc = .Null.
      .Quit(wdDoNotSaveChanges)
    EndWith
    this.Enabled = .F.
*    Clear Events
  Endproc
Enddefine
Cetin
Çetin Basöz

The way to Go
Flutter - For mobile, web and desktop.
World's most advanced open source relational database.
.Net for foxheads - Blog (main)
FoxSharp - Blog (mirror)
Welcome to FoxyClasses

LinqPad - C#,VB,F#,SQL,eSQL ... scratchpad
Previous
Next
Reply
Map
View

Click here to load this message in the networking platform