OLE Automation启动MS Word并展示在最前面 [英] OLE Automation to launch MS Word and bring to front

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

问题描述

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

...
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的最小化图标).

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

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

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

解决方案

Word(与此相关的Excel)仅注册加载到ROT(运行对象表)中的非常实例.

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

这会让您看到自己的行为.

不幸的是,如果没有一些API枚举来枚举系统上所有打开的窗口,您总是会冒着GETOBJECT无法获得期望的对象的风险.

解决这个问题的方法不多.

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

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

您想与哪个连接"并带到最前面.除非您对打开的特定文档"窗口特别感兴趣,否则无论如何这都是一掷骰子.

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

这是文档的GetObject的示例

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

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

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

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

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 (and Excel for that matter) will ONLY register the VERY FIRST INSTANCE that loads in the ROT (Running object table).

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.

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.

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.

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.

Here's an example of GetObject for a document

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

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

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