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 SkipVought on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Word Automation - Template Error

Status
Not open for further replies.

dunc0029

Programmer
Jan 9, 2003
45
0
0
US
I have a frustrating problem with automating Word that I am unable to reproduce. One of my users is getting this error, but I am not, and theoretically, we should have the same pc setup. I basically print a mail-merge Word document on the fly from FoxPro. Here is my code snippet:

IF MESSAGEBOX("Do you want to print the proposal?",36,"Print?")=6
oWord = CREATEOBJECT("word.application")
oword.DisplayAlerts = .f.
oWord.Documents.Open(cWordFile)
oWord.PrintOut()
oWord.Documents.Save(.t.)
oWord.Documents.Close(0)
oWord.quit()
oWord =.NULL.
RELEASE oWord
ENDIF

I added the DisplayAlerts line, the close line, and changed the save line in an effort to kill a fly with a jackhammer and it hasn't worked. I don't even need to save it as there are no changes made, I'm just printing the merged document. When she tries to run this process, she gets a cyptic OLE error first, then clicks ok and the proposal prints fine. Then, Word does not close properly, stalling at the "save as" dialog for Normal.dot (thus, all my "force" code)! Again, I can run this same code and use her same document fine with no prompt and no error. Any suggestions? Is it something in her install or configuration of Word? Thanks in advance!

Jason
 
dunc0029
Code:
oWord = Createobject("word.application")
oWord.Documents.Open("c:\hello2.doc")
oWord.PrintOut()
oWord.Quit()
oWord =.Null.
Release oWord


[b][i]Mike Gagnon[/i][/b]

[i]If you want to get the best response to a question, please check out FAQ184-2483 first.[/I]
 
dunc0029,
Like you, I continue to wrestle with automation issues. The following code 1) opens an existing Word Document which has merge fields, 2)opens a different data source than the one attached to the document, 3) Merges the data, 4) Saves the document to a new filename, 5) Prints the merged document, 6) Closes all documents and 7) Quits Word. Good Luck

***************
&&name of form document with merge fields
ofile="c:\somefolder\somedocument.doc"

&&name of data source
odata="C:\somefolder\mergedata.txt"

&&name of new file to be created
nfile="c:\adapttemp\faxdoc.doc"

&&opens Word in VFP
oWord = createobject("Word.Application")

&&does not display Word
oWord.visible = .f.

&&opens form document
oDocument=oword.Documents.open(ofile)

&& Establishes merge document as formletter
oDocument.MailMerge.MainDocumentType= 0

&& Changes Data Source to odata
oDocument.MailMerge.OpenDataSource(odata)

&&Executes merge
oDocument.MailMerge.execute()

&&Saves merged document
oWord.ActiveDocument.SaveAs(nfile)

&&Prints merged document
oWord.ActiveDocument.printout

&&closes nfile
oword.ActiveDocument.close()

&&closes ofile
oword.ActiveDocument.close()

&&closes Word
oWord.Quit
********************

Hope this helps.

Alan Arons [ponder]
 
Thanks for the replies.

Mike - I at least need a Save(.t.) in there or Word won't close properly. It will get stuck at the Save prompt since Word thinks the document has changed when it merges the data.

Alan - thanks for the code.

I am not merging to a new document as there is only one record in the export file. Do you think merging to a new document would solve my issue? Again, this works beautifully for me on my PC as is (even using her same Word document!). For some reason, she is getting a prompt in Word as if she made changes to the "Normal.dot" template! Any idea how that could be happening? Would other instances of Word being open somehow interfere with the process? I'm about out of ideas to try.

Thanks again!

Jason
 
Jason,
Is it possible that the users normal.dot is corrupted or perhaps she has a macro virus?
 
MadTown - that's entirely possible. Do you know of a way to check that out? We should be running McAfee on all our pcs. I work for BlueCross of MN, so my hands are tied on a lot of things... if we wanted to have Word reinstalled, it would probably take some convincing and about a week... so it would be great to be able to show them proof.
 
Thanks A TON! For some reason, searching the Microsoft Knowledgebase didn't occur to me this time... I'll post back if this solves her problem. Thanks again!
 
dunc0029

Mike - I at least need a Save(.t.) in there or Word won't close properly. It will get stuck at the Save prompt since Word thinks the document has changed when it merges the data.

On my system, the code I suggested, does not bring up the save prompt, no prompt, just close. On my system if I leave the save, it actually brings up Word itself. Windowx XP, Word XP.


Mike Gagnon

If you want to get the best response to a question, please check out FAQ184-2483 first.
 
Hmmm - we're still on Office 97 and Windows 2000... I'll have to keep that in mind when/if we move to XP. Was your document a mail merge document? I get the same behavior if I just open my mail merge document from explorer and try to quit (even though nothing has changed). Maybe this was fixed after '97?
 
dunc0029

No just a regular document, but I dont' think a mail merge would make a difference. It is possible as suggested above that your .dot is damaged. I had that recently, where everytime I closed Word (even in a blank document), it woudl prompt, until I deleted the dot, and recreated it.

Mike Gagnon

If you want to get the best response to a question, please check out FAQ184-2483 first.
 
Unfortunately, my user is still getting this error. She says she has had other issues with MS Office and may just try to get a reimage and/or new pc now since no one has been able to figure it out. I tried it from another user's PC and it works fine, as it does from my PC. She is going to show another underwriter how to do this process, so we are anxious to see if it works for her. If so, I guess I'll just chalk it up to a virus or corrupted Office install. In any case, the workaround is just to have the users bring up Word manually and print... not a huge deal in this case. ...very frustrating for me, though.

Jason
 
Just a followup. This issue has finally been resolved. The user had Word reinstalled and that has fixed the problem. She either had some kind of virus or a bad install of MS Office.

Thanks for all the suggestions!
 
Word automation can be fun, and can be effective as well (I use word automation to perform spelling and grammer checks in edit boxes) but you might want to consider doing your mail-merge in a FoxPro report. Try the textmerge capability of Fox. You create the header of the letter using standard report objects and then use a text box (stretch with overflow checked) for the body of the letter. In the expression field you can use a simple variable like gcLetterBody
 
This is an old positing.

My Question: Where do I learn about these commands such as oWord.ActiveDocument.SaveAs(nfile), etc.?

In other words, I guess I am asking where do I read about the methods and properties for an Excel or Word object?

Thanks,
John
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top