5

I want to my Word Application to come to the Foreground when automation has completed.

The equivalent in Excel is straight forward - the Excel Application object has a .Hwnd property which you can use in conjunction with the Windows API :

SetForegroundWindow((IntPtr)excelApp.Hwnd);

However the Word application does not have a .Hwnd property.

I've tried using Activate() in this sequence:

wordDoc.Activate();
wordApp.Activate();

but this does not work.

I've had a look at finding the process using the application name, but there could be more than one copy of Word running.

Thanks

Joe

Joe.Net
  • 1,175
  • 5
  • 19
  • 36

1 Answers1

5

You may need to iterate the processArray beyond the first. With word 2010 only one WinWord shows in the task manager no matter how many instances are open.

System.Diagnostics.Process[] processArray =
    System.Diagnostics.Process.GetProcessesByName("WinWord");
System.Diagnostics.Process word = processArray[0];
SetForegroundWindow(word.MainWindowHandle);
Olivier Jacot-Descombes
  • 104,806
  • 13
  • 138
  • 188
Rich Wawrzonek
  • 189
  • 4
  • 15
  • +1 for the info, but I have to interject: that is definitely not true about Word 2010+ only ever spawning one "WinWord" process -- just calling "new Application(...)" twice is all it takes. – BrainSlugs83 Nov 24 '13 at 22:07