Word.Application.Activate() fails

Add-in Express™ Support Service
That's what is more important than anything else

Word.Application.Activate() fails
 
Patrick Jox


Guest


Hi,
I have the problem that the Application.Activate() method fires an System.RuntimeInteropServices.COMException saying "The application cannot be activated". No idea why.

This happens after having done the following:
- Open a new instance of word
- create a new document from template
- set Visible to true
- execute Activate() method

After that word remains visible, and when I start again the document creation, now everything works fine. When I am trying to debug the solution everything works ok too?

I am devloping for the .NET Framework 2.0 using Windows 7, Office 2010 and Visual Studio 2010.

Any ideas?

Kind regards
Patrick
Posted 11 Jul, 2010 10:26:21 Top
Patrick Jox


Guest


Because of the described behavior (no error in debug mode) I was suspicious that this could be a timing / synchronization problem and it seems that is the fact.

I had the following code:



Word.Document wd = null;
object missing = Missing.Value;
object template = null;

template = @"C:TempTemp.dotx";

wd = wa.Documents.Add(ref template,
   ref missing, ref missing, ref missing);

wa.Visible = true;
wa.Activate();

return wd;


I inserted some messageboxes to showup in the code (no error) and then deleted them until I got the error again. So it is sufficient to have one messagebox at the beginning to keep the code running. Means display the messagebox after instancing word solves this problem.

Can it be that Word 2010 starts asynchronously? And if yes what can I do wait until Word has completed its startup routines?

Kind regards
Patrick
Posted 12 Jul, 2010 01:50:41 Top
Andrei Smolin


Add-in Express team


Posts: 18793
Joined: 2006-05-11
Hi Patrick,

Try waiting for DocumentOpen, DocumentChange or WindowActivate.


Andrei Smolin
Add-in Express Team Leader
Posted 12 Jul, 2010 02:49:18 Top