为什么 Word 不“出现在前面”?当我们激活它时?

发布于 2024-10-16 21:55:29 字数 298 浏览 5 评论 0原文

我们的 winforms 应用程序与 MS Word 交互,当生成文档时我们运行此代码,并且我们希望在我们的应用程序前面的 Word 中显示它:

[setup w as a Word interop object]

w.Visible = True
w.Activate()

当推出到运行 Office 2007 的 XP 计算机时,它的工作原理如下故意的。

在运行 Office 2010 的 Win7 计算机上,文档会加载到我们的应用程序后面并在任务栏上闪烁。

有什么想法吗?

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?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

猥琐帝 2024-10-23 21:55:29

我最近偶然发现了类似的问题。我的.NET程序称为COM应用程序,但在Win7上它有时既不会显示在任务栏中,也不会显示在桌面上。我无法真正找出原因,但我编写了以下函数来解决该问题:

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

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);
    }
}
心清如水 2024-10-23 21:55:29

将应用程序从带有 Word 2002&3 的 XP 转换为带有 Word 2010 的 Win 7 时遇到同样的问题。发现您打开的第一个文档出现以下情况,之后任何新文档都会在任务栏中闪烁。

打开Word文档后:

document.Activate();
mWordApplication.Activate();

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

策略是转到显示文档的窗口。最小化和最大化会将文档的窗口置于最前面。

您可以对应用程序对象执行相同的操作(如此处建议的 http: //www.access-programmers.co.uk/forums/showthread.php?t=173871 注意:如果窗口一开始就最大化,则最大化而不最小化没有帮助),但是如果您有很多Word打开文档,您会认为您已经赢得了 Windows 中的纸牌游戏...

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...

稳稳的幸福 2024-10-23 21:55:29

我不是专家,但我遇到了同样的问题并在这里找到了方法。我无法获得任何其他解决方案的工作,但我刚刚在这里找到了我的问题的答案...

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

我刚刚在我的代码中添加了一行,如下所示(粗斜体行),Word 文档在 Win 7 上的 Excel 前面打开运行 Office 2010 的计算机:

Dim wordApplication

Set wordApplication = CreateObject("Word.Application")

Application.ActivateMicrosoftApp xlMicrosoftWord

有关其工作原理的更多信息,请访问上面的链接。

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.

大姐,你呐 2024-10-23 21:55:29

我发现的最简单的方法是

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

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);
無處可尋 2024-10-23 21:55:29

w.Visible = True
w.Activate()

对我来说很好用!

看看其他原因。

例如

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

文档出现在前面。

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文