Joe Smith
Joe Smith

Reputation: 101

Interop.word open document in foreground

I am attempting to open an existing word document and have the MS Word window be brought to the front. I have used the following code.

var app = new Application{ Visible = true};
app.Documents.Open(path);
app.Activate();

This opens the document in word and on my windows 7 box brings word 2013 to the foreground. On an end user machine running windows 8.1 and office 2010 it opens the document but does not bring Word to the front. Are there different/missing steps needed for windows 8 or office 2010?

Thanks

Upvotes: 2

Views: 847

Answers (1)

jhmt
jhmt

Reputation: 1421

Try Microsoft.VisualBasic.Interaction.AppActivate

Microsoft.VisualBasic.Interaction.AppActivate(app.Caption);

If this doesn't work, then try SetForegroundWindow API like this:

[DllImport("user32.dll")]
[return: MarshalAs(UnmanagedType.Bool)]
static extern bool SetForegroundWindow(IntPtr hWnd);

[DllImport("user32.dll", SetLastError = true)]
static extern System.IntPtr FindWindow(string lpClassName, string lpWindowName);

private void ShowForeground(Word.Application app)
{
    IntPtr handler = FindWindow(null, app.Caption);
    SetForegroundWindow(handler);
}

Upvotes: 1

Related Questions