Link to home
Start Free TrialLog in
Avatar of MichaelKatz
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.Application")

*-*  Document file location and file name
oDocument = oWord.Documents.ADD( gcTxtDocFile )

*-*  && .dat file location and file name
oWord.ActiveDocument.MailMerge.OpenDataSource( 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
Avatar of Cyril Joudieh
Cyril Joudieh
Flag of Lebanon image

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:

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

Open in new window

Avatar of MichaelKatz
MichaelKatz

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.
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(FindWindow(0, oword.ActiveWindow.Caption + ' - ' + oword.Caption))
Ah if you have the file existing then you need to use Open and not Add.
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.Application")
oDocument = oWord.Documents.ADD( gcTxtDocFile )  oWord.ActiveDocument.MailMerge.OpenDataSource( gcTxtDatFile )

oWord.ACTIVATE()

oWord.VISIBLE = .T.


oWord.ActiveDocument.SAVEAS( 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.MailMerge.State = wdMainAndDataSource
            oWord.ActiveDocument.MailMerge.Destination = wdSendToPrinter
            oWord.ActiveDocument.MailMerge.Execute
        ENDIF

        *-* Reset windows default printer
        oWord.ActivePrinter = lcWindowsDefaultPrinter

        FOR nCopies = 1 TO THISFORM.spnNumOfCopies.VALUE - 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(wdDoNotSaveChanges)  && 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
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?
pcelba

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.
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
SOLUTION
Avatar of Pavel Celba
Pavel Celba
Flag of Czechia image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
ASKER CERTIFIED SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
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.
This has to be from a previously started Word.

By the way: You can also use GetObject(,"Word.Application") 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.
Only Excel can be opened multiple times without affecting each other. Word and PowerPoint disturb the user's work.
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.
.. 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
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.