Why doesn't Word “come to the fore” when we activate it?

The winforms application interacts with MS Word, and we run this code when creating the document, and we want to show it in Word before our application:

[setup w as a Word interop object] w.Visible = True w.Activate() 

When deployed to XP computers running Office 2007 , this works as intended.

On Win7 machines running Office 2010 , the document loads for our application and blinks on the taskbar.

Any ideas?

+7
source share
4 answers

I recently came across a similar problem. My .NET program was called a COM application, but on Win7 it sometimes did not appear on the taskbar or on the desktop at all. I really could not track the cause of this, but I wrote the following function to solve this problem:

 [System.Runtime.InteropServices.DllImport("User32.dll")] private static extern bool SetForegroundWindow(IntPtr hwnd); private static void BringAppToFront() { foreach (var p in System.Diagnostics.Process.GetProcesses().Where(p => p.ProcessName == "COMInstanceName")) { if (p.MainWindowHandle.ToInt32() != 0) SetForegroundWindow(p.MainWindowHandle); } } 
+7
source

I had the same problem when converting an application from XP with Word 2002 and from 3 to Win 7 with Word 2010. We found the following works for the first document to open, after which new documents will appear on the taskbar.

After opening a Word document:

 document.Activate(); mWordApplication.Activate(); foreach (Word.Window window in document.Windows) { window.WindowState = Word.WdWindowState.wdWindowStateMinimize; window.WindowState = Word.WdWindowState.wdWindowStateMaximize; } 

The strategy should go after the window in which the document is displayed. Minimization and maximization will bring the document window to the front.

You can do the same with the application object (as suggested here http://www.access-programmers.co.uk/forums/showthread.php?t=173871 note: maximizing without minimizing will not help if the window is maximized to start) but if you have many Word documents, you will think that you won the solitare game on Windows ...

+2
source

I am not an expert, but I got into the same problem and found my way here. I could not get any other solutions to work, but I just found the answer to my problem here ...

http://david.gardiner.net.au/2010/05/bad-old-days-of-vba-and-opening-word.html

I just added one line as follows (line in bold italics ) to my code and Word documents that opened before Excel on Win 7 computers running Office 2010:

Smart Word

Set wordApplication = CreateObject ("Word.Application")

Application.ActivateMicrosoftApp xlMicrosoftWord

Additional information on why this works is at the link above.

+1
source

w.Visible = True w.Activate ()

Works great for me !!!

See other reasons.

eg

  Dim oWord As Microsoft.Office.Interop.Word.Application = New Microsoft.Office.Interop.Word.Application Dim oDoc As Microsoft.Office.Interop.Word.Document = oWord.Documents.Open(Path) Dim range As Microsoft.Office.Interop.Word.Range = oDoc.Range range.Find.Execute("[NUM]", False, False, , , , , , , _NUM_, 2, False, ) oWord.Visible = True oWord.Activate() 

The document comes forward.

0
source

All Articles