OLE 自动化启动 MS Word 并置于前台 [英] OLE Automation to launch MS Word and bring to front

查看:19
本文介绍了OLE 自动化启动 MS Word 并置于前台的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

连接到正在运行的 MS Word 实例并将此应用程序置于最前面的正确"(推荐)方法是什么?我正在从 VBA 应用程序执行以下操作:

What is the "correct" (recommended) method for connecting to a running instance of MS Word and bringing this application to the front? I am doing something like the following from a VBA app:

...
objWord = GetObject ("Word.Application")
if (objWord is nothing) then
  objWord = CreateObject("Word.Application")
end if
objWord.Activate()
objWord.Visible = true
objWord.WindowState = 1 'maximized
...

在带有 Word 2007 的 Windows XP 上运行,这在大多数情况下都有效 - 但有时无法将 Word 窗口置于最前面(而是在任务栏中闪烁最小化的 Word 图标).

Running on Windows XP with Word 2007, this works most of the time - but periodically, fails to bring the Word window to the front (and instead flashes the minimized icon for Word in the task bar).

注意:我通过使用 FindWindow Win API 调用部分解决了这个问题:

NOTE: I partially resolved this issue by using the FindWindow Win API call:

hwnd = FindWindow("OpusApp", vbNullString)
If hwnd > 0 Then 
  SetForegroundWindow (hwnd)
end if

这不是 100%,因为(正如 drventure 所指出的),如果多个 Word 实例正在运行,您无法确定您将获得哪个句柄.因为当我的代码启动 Word 时,它首先使用 GetObject,如果失败,则使用 CreateObject,只要有一个 Word 实例开始运行,我就可以了.

This is not 100% because (as drventure pointed out), if multiple instances of Word are running, you cannot be certain which you will get a handle to. Since when my code launches Word it uses GetObject first and then CreateObject if that fails, as long as there is one instance of Word running to start with, I am OK.

推荐答案

Word(和 Excel)只会注册在 ROT(运行对象表)中加载的非常第一实例.

Word (and Excel for that matter) will ONLY register the VERY FIRST INSTANCE that loads in the ROT (Running object table).

ROT 是 GETOBJECT获取对象"的地方,因此,在某些情况下,它可能加载两个 WinWord.exe 实例,一个可见,一个不可见,但不可见实例是在 ROT 中注册的实例可见的不是.

The ROT is where GETOBJECT "gets the object", so, in certain circumstances, its' possible to have two instances of WinWord.exe loaded, one visible, one not, but the invisible instance is the one registered in the ROT and the visible one is NOT.

这会让你看到你看到的行为.

That'll get you the behavior you're seeing.

不幸的是,如果没有一些 API 调用来枚举系统上所有打开的窗口,您总是会面临 GETOBJECT 无法为您提供您期望的对象的风险.

Unfortunately, without some API calls to enumerate all the open windows on the system, you always run the risk that GETOBJECT won't get you the object that you're expecting it to.

没有太多办法解决这个问题.

There's not much of a way around this.

此外,你的最终目标还是有点可疑.

Besides, you're end goal is a bit suspect anyway.

当您说连接到正在运行的 MS Word 实例并将此应用程序置于前端的正确"方法是什么?"时,如果加载了 2 个或更多 Winword.exe 的实际实例,会发生什么情况?

When you say "What is the "correct" method for connecting to a running instance of MS Word and bringing this application to the front?", what happens if there are 2 or more actual instances of Winword.exe loaded?

您希望连接"到哪个并将其带到前面.无论如何,这都是掷骰子,除非您对打开的特定文档窗口特别感兴趣.

Which would you want to "connect" to and bring to the front. that's a roll of the dice anyway, unless you're specifically interested in a particular DOCUMENT window that's open.

据我了解,Word 会在 ROT 中注册所有文档窗口,无论它是哪个 Winword 实例,因此理论上您可以使用 Getobject 来检索特定的 DOCUMENT,从 DOCUMENT 对象中获取 APPLICATION 对象,然后使它可见与正常窗口状态.

From what I understand, Word will register ALL document windows in the ROT, regardless of which instance of Winword it is, so you could theoretically Use Getobject to retrieve a particular DOCUMENT, get the APPLICATION object from the DOCUMENT object and then make it VISIBLE with a NORMAL windowstate.

这是文档的 GetObject 示例

Here's an example of GetObject for a document

http://support.microsoft.com/kb/307216

这篇关于OLE 自动化启动 MS Word 并置于前台的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆