Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/323.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/.net/20.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# Interop.word在前台打开文档_C#_.net_Interop_Ms Office - Fatal编程技术网

C# Interop.word在前台打开文档

C# Interop.word在前台打开文档,c#,.net,interop,ms-office,C#,.net,Interop,Ms Office,我试图打开现有的word文档,并将MS word窗口置于最前面。我使用了以下代码 var app = new Application{ Visible = true}; app.Documents.Open(path); app.Activate(); 这将在word中打开文档,在“我的windows 7”框中,word 2013将出现在前台。在运行windows 8.1和office 2010的最终用户计算机上,它会打开文档,但不会将Word放在前面。 windows 8或office 20

我试图打开现有的word文档,并将MS word窗口置于最前面。我使用了以下代码

var app = new Application{ Visible = true};
app.Documents.Open(path);
app.Activate();
这将在word中打开文档,在“我的windows 7”框中,word 2013将出现在前台。在运行windows 8.1和office 2010的最终用户计算机上,它会打开文档,但不会将Word放在前面。 windows 8或office 2010是否需要不同的/缺少的步骤

谢谢

试试看

如果这不起作用,请尝试以下API:

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