4

We are having a problem experienced by a few users when attempting to launch Word from our application via the office interop:

using Word = Microsoft.Office.Interop.Word;

public void ShowWord()
{
  _word = new Word.ApplicationClass();
  _word.Visible = true;
  _word.Activate();
}

If word is not always open a COM exception is thrown stating "Cannot activate application." Adding a Thread.Sleep(1000) before calling _word.Activate() prevents this, but obviously is not ideal.

public void ShowWord()
{
  _word = new Word.ApplicationClass();
  _word.Visible = true;
  Thread.Sleep(1000)
  _word.Activate();
}

Has anyone seen this before and knows what is causing this and what the right way to fix this is?

MattC
  • 3,984
  • 1
  • 33
  • 49
satnhak
  • 9,407
  • 5
  • 63
  • 81
  • From MSDN: The `ApplicationClass` class supports the .NET Framework infrastructure and is not intended to be used directly from your code. It may prove to have nothing to do with your problem, but you should stop using nonetheless. Use `Application` instead. – João Angelo Mar 22 '11 at 17:55
  • Yes, I was quite confused by this use of `ApplicationClass` too. This is a piece of fairly old code; I'll take a look at `Application` to see if that solves the problem. – satnhak Mar 23 '11 at 09:27
  • I've changed everything to `Application` but it makes no difference at all. – satnhak Mar 23 '11 at 09:52
  • related: [why-doesnt-word-come-to-front-when-we-activate-it](http://stackoverflow.com/questions/4955366/why-doesnt-word-come-to-front-when-we-activate-it) – nawfal Jan 04 '14 at 16:35

2 Answers2

7

We encountered a similar issue, and it seems that Word is asynchonously waiting for the OS to show its window. The way we resolved this is by waiting until the Visible property returns true:

public void ShowWord()
{
  _word = new Word.Application();
  _word.Visible = true;

  System.Diagnostics.Stopwatch sw = System.Diagnostics.Stopwatch.StartNew();
  while (!_word.Visible && sw.ElapsedMilliseconds < 10000)
  { /* Just Wait!! (at most 10s) */}
  _word.Activate();
}

Hope this helps somebody.

Simon Gregory
  • 566
  • 7
  • 10
  • Not tested this as this was way ages ago, but I'll accept on the basis that it sounds pretty plausible :) – satnhak Oct 04 '13 at 17:26
0

Does your application have permission to activate the Word COM object?

Check in DCOMCNFG what the local activation security requirements are.

However, not sure why your Thread.Sleep(1000)would allow it to work?

MattC
  • 3,984
  • 1
  • 33
  • 49
  • Yes, this is not a permissions problem. It is to do with the object taking time to initialise internally. So I would expect `new` to be synchronous. But it seems to be asynchronous. – satnhak Mar 23 '11 at 09:26
  • AFAIK, calls to COM via interop are serialized to a single thread, which would make asynchronous behavior a little strange. Does anything get written to the event log? – MattC Mar 23 '11 at 09:36
  • No there is nothing in the event log. – satnhak Mar 23 '11 at 10:38
  • I can only think then that the Thread.Sleep(1000) is as you say, allowing a context switch back to the COM interop thread which then completes it's init before handing back to your Activate call on your calling thread. If you debug what do you see in the Thread window? – MattC Mar 23 '11 at 10:58
  • Therein lies the problem, we cannot reproduce this issue on a development machine; it's only been seen on a couple of our laptops. We've made a VM from one of these laptops and have that running on a dev machine, but I've go no idea how I could attach to that process. – satnhak Mar 23 '11 at 12:59
  • If the VM is on the network then you could install the remote debugging tools. You then run it on the VM and then you will be able to remote debug that instance from within VS. You need to log into both machines as the same account I think and you will need admin rights on both. – MattC Mar 23 '11 at 13:07
  • Have you already seen this: http://stackoverflow.com/questions/2690180/com-exception-0x800a11f9-cannot-activate-application – MattC Mar 23 '11 at 13:13
  • Yes, as I've said, this is not a permissions issue: adding a `Thread.Sleep(1000)` solves the issue. I probably should have mentioned that despite this error message Word does actually launch, but the object is invalid for a few hundred milliseconds. – satnhak Mar 23 '11 at 17:36
  • What is the target OS and version of office: I found this. http://social.msdn.microsoft.com/Forums/en/vsto/thread/bc1045ea-4a11-4e37-8a95-44a0f53e86b2 which seems to suggest that "Activate() method does not work properly on Vista"?? – MattC Mar 24 '11 at 08:10
  • Windows 7. But, that is not the issue; the fail of activate just fails to bring the window to the front (which doesn't happen on Windows 7), there is no exception thrown by the call. – satnhak Mar 24 '11 at 09:06