MichaelKatz
asked on
OLE AUTOMATION. MS WORD ERROR 1429. CANNOT ACTIVATE APPLICATION
I have 2 separate applications that use ole automation to pass mail merge documents to MS word.
The relevent code is:
[========================= ========== ========== =]
*-* Setup and activate Word
oWord = CREATEOBJECT("Word.Applica tion")
*-* Document file location and file name
oDocument = oWord.Documents.ADD( gcTxtDocFile )
*-* && .dat file location and file name
oWord.ActiveDocument.MailM erge.OpenD ataSource( gcTxtDatFile )
oWord.ACTIVATE()
[========================= ========== ========== ====]
The last line throws:
OLE IDispatch exception code 0 from Miocrosoft Word: Cannot activate application.
What I can't figure out is that it only fails in one of the apps. the other one works fine. I copied the form and Word files from 1 app to the other and made a few changes to filenamse and paths.
The files exist. I even tried the 2 files from the working app and still get the error.
I have run out of ideas of what to look for.
Any suggestions are appreciated.
Michael
The relevent code is:
[=========================
*-* Setup and activate Word
oWord = CREATEOBJECT("Word.Applica
*-* Document file location and file name
oDocument = oWord.Documents.ADD( gcTxtDocFile )
*-* && .dat file location and file name
oWord.ActiveDocument.MailM
oWord.ACTIVATE()
[=========================
The last line throws:
OLE IDispatch exception code 0 from Miocrosoft Word: Cannot activate application.
What I can't figure out is that it only fails in one of the apps. the other one works fine. I copied the form and Word files from 1 app to the other and made a few changes to filenamse and paths.
The files exist. I even tried the 2 files from the working app and still get the error.
I have run out of ideas of what to look for.
Any suggestions are appreciated.
Michael
I had similar problems before on Vista and 7 and it's because sometimes when you use Automation, the applications launches under a different user name and has different access privileges and thus cannot access the file.
Not an explanation, but do you actually need to activate?
I last used Foxpro in DOS, so there might be syntax errors, but try this:
I last used Foxpro in DOS, so there might be syntax errors, but try this:
*-* Setup and activate Word
oWord = CREATEOBJECT("Word.Application")
*-* Document file location and file name
oDocument = oWord.Documents.ADD( gcTxtDocFile )
*-* && .dat file location and file name (using document object)
oDocument .MailMerge.OpenDataSource( gcTxtDatFile )
oDocument.MailMerge.Execute
ASKER
I am working on a stand alone desktop, as administrator.
As I mentioned . . .
I even tried the 2 files from the working app and still get the error.
So, the files are there and I have access.
As I mentioned . . .
I even tried the 2 files from the working app and still get the error.
So, the files are there and I have access.
It is not possible to activate Word if it is not visible, so try:
oWord.Visible = .T.
before the Activate
If it does not help use following code to activate the window:
DECLARE INTEGER FindWindow IN user32;
STRING lpClassName,;
STRING lpWindowName
DECLARE INTEGER SetForegroundWindow IN user32 INTEGER hwnd
? SetForegroundWindow(FindWi ndow(0, oword.ActiveWindow.Caption + ' - ' + oword.Caption))
oWord.Visible = .T.
before the Activate
If it does not help use following code to activate the window:
DECLARE INTEGER FindWindow IN user32;
STRING lpClassName,;
STRING lpWindowName
DECLARE INTEGER SetForegroundWindow IN user32 INTEGER hwnd
? SetForegroundWindow(FindWi
Ah if you have the file existing then you need to use Open and not Add.
ASKER
Graham,
I need to activate because the user has a choice before activation to Print, Preview, Edit, or Create a file.
If I bypass activation, some later code regarding active printer fails.
pcelba,
This is pretty much the code that works in one of the apps. I moved .Visible in front of .Activate ; no difference.
[========================= ========== =]
* Setup and activate Word
oWord = CREATEOBJECT("Word.Applica tion")
oDocument = oWord.Documents.ADD( gcTxtDocFile ) oWord.ActiveDocument.MailM erge.OpenD ataSource( gcTxtDatFile )
oWord.ACTIVATE()
oWord.VISIBLE = .T.
oWord.ActiveDocument.SAVEA S( gcTxtDocFile )
*-* Print or preview
IF UPPER(ProcessWanted) = 'PRINT'
* Merge the doc with the data, print to default printer and close
* Word is run in the background.
oWord.WINDOWSTATE = wdWindowStateMinimize
IF oWord.ActiveDocument.MailM erge.State = wdMainAndDataSource
oWord.ActiveDocument.MailM erge.Desti nation = wdSendToPrinter
oWord.ActiveDocument.MailM erge.Execu te
ENDIF
*-* Reset windows default printer
oWord.ActivePrinter = lcWindowsDefaultPrinter
FOR nCopies = 1 TO THISFORM.spnNumOfCopies.VA LUE - 1
*-* MSWord apparently resets to the Windows default printer, so . .
oWord.ActivePrinter = '&xSelectedPrinter'
oWord.printout() && Prints 1 copy
NEXT
*-* Print and close
oWord.ActivePrinter = '&xSelectedPrinter'
oWord.QUIT(wdDoNotSaveChan ges) && Prints 1 copy, close file, no save
RELEASE oWord
RELEASE oDocument
THISFORM.RELEASE
[========================= ========== ==]
Captain Cyril,
I tried OPEN instead of ADD with no difference.
All who are trying to help me . . .
I am using identical code in 2 different apps on the same machine. One work and one doesn't.
Help!
Michael
I need to activate because the user has a choice before activation to Print, Preview, Edit, or Create a file.
If I bypass activation, some later code regarding active printer fails.
pcelba,
This is pretty much the code that works in one of the apps. I moved .Visible in front of .Activate ; no difference.
[=========================
* Setup and activate Word
oWord = CREATEOBJECT("Word.Applica
oDocument = oWord.Documents.ADD( gcTxtDocFile ) oWord.ActiveDocument.MailM
oWord.ACTIVATE()
oWord.VISIBLE = .T.
oWord.ActiveDocument.SAVEA
*-* Print or preview
IF UPPER(ProcessWanted) = 'PRINT'
* Merge the doc with the data, print to default printer and close
* Word is run in the background.
oWord.WINDOWSTATE = wdWindowStateMinimize
IF oWord.ActiveDocument.MailM
oWord.ActiveDocument.MailM
oWord.ActiveDocument.MailM
ENDIF
*-* Reset windows default printer
oWord.ActivePrinter = lcWindowsDefaultPrinter
FOR nCopies = 1 TO THISFORM.spnNumOfCopies.VA
*-* MSWord apparently resets to the Windows default printer, so . .
oWord.ActivePrinter = '&xSelectedPrinter'
oWord.printout() && Prints 1 copy
NEXT
*-* Print and close
oWord.ActivePrinter = '&xSelectedPrinter'
oWord.QUIT(wdDoNotSaveChan
RELEASE oWord
RELEASE oDocument
THISFORM.RELEASE
[=========================
Captain Cyril,
I tried OPEN instead of ADD with no difference.
All who are trying to help me . . .
I am using identical code in 2 different apps on the same machine. One work and one doesn't.
Help!
Michael
So my next recommendation: You have to avoid asynchroneous Word execution problems. Try to wait a few seconds before the Word activation (e.g. WAIT WINDOW "..." TIME 5).
The same piece of code in two different apps does not necessarily work same way under MS Windows :-)...
SetForeGroungWindow should replace the activation. Did you try it?
The same piece of code in two different apps does not necessarily work same way under MS Windows :-)...
SetForeGroungWindow should replace the activation. Did you try it?
ASKER
pcelba
I tried it. I t doesn't solve it. I need to activate Word so the user can modify the document(s).
Michael
I tried it. I t doesn't solve it. I need to activate Word so the user can modify the document(s).
Michael
When I last had problems with automation, it was about the non existance of sub structure like the documents collection or such things. What helped is to put DOEVENTS FORCE after CREAETOBJECT calls and other things needing draining of the event queue, before next things can happen.
It's worth a try but maybe also not helping. Theres gotta be an essential difference, even though Pavel also is right the same code can work differently, the simples example is you lock a file, of course that works in the first run, but not in another run, and it's the same code. See if you use any resources a second or parallel run can't use.
Bye, Olaf.
It's worth a try but maybe also not helping. Theres gotta be an essential difference, even though Pavel also is right the same code can work differently, the simples example is you lock a file, of course that works in the first run, but not in another run, and it's the same code. See if you use any resources a second or parallel run can't use.
Bye, Olaf.
ASKER
Olaf,
I was checking if one of the apps was using a resource not availabe to the other one and discovered that in the app that worked, there was an error handling routine for error # 1429..
I sstill get the error "Cannot Activate Application", but I believe the error is wrong. Word is activated, because I can control the printers; it prints and allows editing, and it shows up in the task manager as a process, NOT AN APPLICATION.
Does anyone know why I get the error when Word is actually activated ?
Michael
I was checking if one of the apps was using a resource not availabe to the other one and discovered that in the app that worked, there was an error handling routine for error # 1429..
I sstill get the error "Cannot Activate Application", but I believe the error is wrong. Word is activated, because I can control the printers; it prints and allows editing, and it shows up in the task manager as a process, NOT AN APPLICATION.
Does anyone know why I get the error when Word is actually activated ?
Michael
SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
Absolutely correct. I cannot activate Word without making it visible first.
What is interesting is that it says it is not activated, but I do get an editable document to work with.
Anyway, This issue has been resolved to my satisfaction and I also learned some new stuff.
Thanks.
Michael
What is interesting is that it says it is not activated, but I do get an editable document to work with.
Anyway, This issue has been resolved to my satisfaction and I also learned some new stuff.
Thanks.
Michael
>What is interesting is that it says it is not activated, but I do get an editable document to work with.
This has to be from a previously started Word.
By the way: You can also use GetObject(,"Word.Applicati on") to get a reference to an already started Word. And that can then be activated, if it's already visible.
It's not advisable to do automation of a word instance already running, as that may simply disturb the user from using it in parallel to your automation. I'd recommend to keep the Word instance invisible while you still work with it, as that is faster than when it's visible and prevents a user from shutting it down or intercept in any other way, while you work with it.
There may be situations you want to work on an already started Word, but that'll surely not involve something like Mailmerge.
Bye, Olaf.
This has to be from a previously started Word.
By the way: You can also use GetObject(,"Word.Applicati
It's not advisable to do automation of a word instance already running, as that may simply disturb the user from using it in parallel to your automation. I'd recommend to keep the Word instance invisible while you still work with it, as that is faster than when it's visible and prevents a user from shutting it down or intercept in any other way, while you work with it.
There may be situations you want to work on an already started Word, but that'll surely not involve something like Mailmerge.
Bye, Olaf.
Only Excel can be opened multiple times without affecting each other. Word and PowerPoint disturb the user's work.
ASKER
Olaf,
I rebooted my desktopp and went immediately to the app and tried to activate word without first making it visible.
When I got the 1429 error I choose retry and got a running version of Word.
There is a line . . . oWord.VISIBLE = .T.
I rebooted my desktopp and went immediately to the app and tried to activate word without first making it visible.
When I got the 1429 error I choose retry and got a running version of Word.
There is a line . . . oWord.VISIBLE = .T.
ASKER
.. Continued.
Olaf,
I rebooted my desktopp and went immediately to the app and tried to activate word without first making it visible.
When I got the 1429 error I choose retry and got a running version of Word.
There is a line . . . oWord.VISIBLE = .T.
farther down that may be the reason. But, if the activation actually failed. would the Visible command be any good.
also, VARTPE returns "O"
Michael
Olaf,
I rebooted my desktopp and went immediately to the app and tried to activate word without first making it visible.
When I got the 1429 error I choose retry and got a running version of Word.
There is a line . . . oWord.VISIBLE = .T.
farther down that may be the reason. But, if the activation actually failed. would the Visible command be any good.
also, VARTPE returns "O"
Michael
Vartype returns "O" from the beginning. The error you see may not stop VFP from running oWord.VISIBLE = .T. and so clicking retry doesn't fail. What do I know? I only know you never get this error, when you turn the order around and make Word visible first. So why bother with anything else?
Bye, Olaf.
Bye, Olaf.