【问题标题】:Interop.word open document in foregroundInterop.word 在前台打开文档
【发布时间】:2015-10-28 00:27:34
【问题描述】:

我正在尝试打开现有的 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 是否需要不同的/缺少的步骤?

谢谢

【问题讨论】:

    标签: c# .net interop ms-office


    【解决方案1】:

    试试Microsoft.VisualBasic.Interaction.AppActivate

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

    如果这不起作用,请尝试SetForegroundWindow 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);
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多