Solved

OLE AUTOMATION.  MS WORD ERROR 1429.  CANNOT ACTIVATE APPLICATION

Posted on 2013-01-24
18
2,694 Views
Last Modified: 2013-01-28
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
0
Comment
Question by:MichaelKatz
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 7
  • 4
  • 3
  • +2
18 Comments
 
LVL 27

Expert Comment

by:CaptainCyril
ID: 38814898
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.
0
 
LVL 76

Expert Comment

by:GrahamSkan
ID: 38814997
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

0
 

Author Comment

by:MichaelKatz
ID: 38815017
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.
0
Webinar: Aligning, Automating, Winning

Join Dan Russo, Senior Manager of Operations Intelligence, for an in-depth discussion on how Dealertrack, leading provider of integrated digital solutions for the automotive industry, transformed their DevOps processes to increase collaboration and move with greater velocity.

 
LVL 42

Expert Comment

by:pcelba
ID: 38815066
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))
0
 
LVL 27

Expert Comment

by:CaptainCyril
ID: 38815096
Ah if you have the file existing then you need to use Open and not Add.
0
 

Author Comment

by:MichaelKatz
ID: 38815350
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
0
 
LVL 42

Expert Comment

by:pcelba
ID: 38816192
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?
0
 

Author Comment

by:MichaelKatz
ID: 38816516
pcelba

I tried it. I t doesn't solve it. I need to activate Word so the user can modify the document(s).

Michael
0
 
LVL 29

Expert Comment

by:Olaf Doschke
ID: 38821669
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.
0
 

Author Comment

by:MichaelKatz
ID: 38825135
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
0
 
LVL 42

Assisted Solution

by:pcelba
pcelba earned 250 total points
ID: 38825268
Error 1429 is common OLE error and you have to look for more info by calling AERROR() function. It will give you OLE exception number which you may then search on the net. But I am skeptic you'll find working solution...

If the application is listed as a process then it means it is running - you shoud see it there immediately after the CREATEOBJECT() call. The presence in applications list just means the main application window is visible. If your observation shows editable Word window which is not listed among applications then I cannot explain it.

In such case you may just enclose the ACTIVATE() call into TRY - CATCH structure as the simplest solution.
0
 
LVL 29

Accepted Solution

by:
Olaf Doschke earned 250 total points
ID: 38825900
I simply tried the two lines:
oWord = CreateObject("Word.Application")
oWord.Activate()

Open in new window

This shows the error you see, but that's clear now. You cannot activate a window, which isn't visible. If you see a word window already, this is from another automation most probably, if you CreateObject("Word.Application") you start a new Word, always. You have to make it visible = .t. before you can activate it, and you may even need to wait until it's really a visible window, before you can call oWord.Activate() and not get an error.
Besides you may also get this error, even when it's visible but busy.

See for yourself:
oWord1 = CreateObject("Word.Application")
oWord1.visible = .t.
oWord1.Activate()
oWord2 = CreateObject("Word.Application")
oWord2.visible = .t.
oWord2.Activate()
oWord3 = CreateObject("Word.Application")
oWord3.Activate() && error, you can't activate an invisible window.

Open in new window


Bye, Olaf.
0
 

Author Comment

by:MichaelKatz
ID: 38826027
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
0
 
LVL 29

Expert Comment

by:Olaf Doschke
ID: 38826596
>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.
0
 
LVL 27

Expert Comment

by:CaptainCyril
ID: 38826639
Only Excel can be opened multiple times without affecting each other. Word and PowerPoint disturb the user's work.
0
 

Author Comment

by:MichaelKatz
ID: 38827216
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.
0
 

Author Comment

by:MichaelKatz
ID: 38827247
.. 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
0
 
LVL 29

Expert Comment

by:Olaf Doschke
ID: 38827681
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.
0

Featured Post

Guide to Performance: Optimization & Monitoring

Nowadays, monitoring is a mixture of tools, systems, and codes—making it a very complex process. And with this complexity, comes variables for failure. Get DZone’s new Guide to Performance to learn how to proactively find these variables and solve them before a disruption occurs.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

PaperPort has a feature called the "Send To Bar". It provides a convenient, drag-and-drop interface for using other installed software, such as Microsoft Office. However, this article shows that the latest Office 2016 apps (installed with an Office …
In this article we discuss how to recover the missing Outlook 2011 for Mac data like Emails and Contacts manually.
This video shows and describes the main difference between both orientations in Microsoft Word. Viewers will understand when to use each orientation and how to get the most out of them.
The viewer will learn how to use the =DISCRINV command to create a discrete random variable, use this command to model a set of probabilities and outcomes in a Monte Carlo simulation, and learn how to find the standard deviation of a set of probabil…

756 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question