vba 如何获取 Word 应用程序的 Hwnd/进程 ID,并将其设置为前台窗口
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/9521161/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How do I get the Hwnd / Process Id for a Word Application, and set as Foreground Window
提问by Joe.Net
I want to my Word Application to come to the Foreground when automation has completed.
我希望我的 Word 应用程序在自动化完成后进入前台。
The equivalent in Excel is straight forward - the Excel Application object has a .Hwnd property which you can use in conjunction with the Windows API :
Excel 中的等效项很简单 - Excel Application 对象有一个 .Hwnd 属性,您可以将其与 Windows API 结合使用:
SetForegroundWindow((IntPtr)excelApp.Hwnd);
However the Word application does not have a .Hwnd property.
但是,Word 应用程序没有 .Hwnd 属性。
I've tried using Activate() in this sequence:
我试过按以下顺序使用 Activate():
wordDoc.Activate();
wordApp.Activate();
but this does not work.
但这不起作用。
I've had a look at finding the process using the application name, but there could be more than one copy of Word running.
我查看了使用应用程序名称查找进程的方法,但可能有多个 Word 副本在运行。
Thanks
谢谢
Joe
乔
采纳答案by Rich Wawrzonek
You may need to iterate the processArray beyond the first. With word 2010 only one WinWord shows in the task manager no matter how many instances are open.
您可能需要在第一个之后迭代 processArray。对于 Word 2010,无论打开多少个实例,任务管理器中都只会显示一个 WinWord。
System.Diagnostics.Process[] processArray = System.Diagnostics.Process.GetProcessesByName("WinWord");
System.Diagnostics.Process word = processArray[0];
SetForegroundWindow(word.MainWindowHandle);