7

Our winforms application interacts with MS Word and we run this code when a document is generated and we want to show it in Word in front of our application:

[setup w as a Word interop object]

w.Visible = True
w.Activate()

When rolled out to XP machines running Office 2007 this works as intended.

On Win7 machines running Office 2010 the document loads behind our application and flashes on the taskbar.

Any ideas?

1
  • 2
    I had the same problem and after doing some research via google it came to the fact that this is an indented behavior of Windows since version 7. This is some kind of security mechanism to prevent other applications to disturb your work. One application (your program which want to interop with word) can't change the z-order of other applications which aren't run in the same process. I can't paste a url with official information about this - I just remember that I found this on the net some when earlier... Commented Jul 24, 2015 at 9:17

5 Answers 5

8

I stumbled upon a similar problem recently. My .NET program called a COM application, but on Win7 it would sometimes neither show up in taskbar nor on the desktop at all. I wasn't really able to track down the cause of this, but I wrote the following function to work around the issue:

[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);
    }
}
0
3

Had the same issue when converting an application from XP with Word 2002&3 to Win 7 with Word 2010. Found the following works for the first document you open, after that any new documents appear in the task bar blinking.

After opening the 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 is to go after the Window in which the document is displayed. Minimizing and maximizing will bring the document's 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: maximize without minimize doesn't help if the window is maximized to begin with), but if you have many Word documents open you'll think you've won a game of solitare in Windows...

1

I'm no expert but I hit this same problem and found my way here. I couldn't get any of the other solutions to work but I just found an 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 (the line in bold italics) to my code and Word docs opened up in front of Excel on Win 7 machines running Office 2010:

Dim wordApplication

Set wordApplication = CreateObject("Word.Application")

Application.ActivateMicrosoftApp xlMicrosoftWord

More information on why this works at the link above.

1

The simplest method I found out is

Word.Application wordInstance = new Word.Application();
...
wordInstance.Visible = true;
SetForegroundWindow(wordInstance.Application.ActiveWindow.Hwnd);

...
[DlImport("User32.dll")]
[return: MarshalAs(UnmanagedType.U4)]
static extern int SetForegroundWindow(int hwnd);
0

w.Visible = True w.Activate()

Works for me fine!!!

See the other reasons.

for example

 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()

Document comes to front.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.