我可以在 Excel 2007 中使用 VBA 命令聚焦到另一个窗口吗?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/9108232/
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
Can I focus to another window with a VBA command in Excel 2007?
提问by CheeseConQueso
I want to run a VBA script from Excel 2007 that can switch focus to another active window (ALT+TAB) send commands to that window, and then return focus to Excel and continue to perform commands in the VBA.
我想从 Excel 2007 运行一个 VBA 脚本,该脚本可以将焦点切换到另一个活动窗口 (ALT+TAB) 向该窗口发送命令,然后将焦点返回到 Excel 并继续在 VBA 中执行命令。
For example:
例如:
Copy the contents of cell A1, focus onto an active Internet Explorer window, send the TAB key command, paste the copied data from A1, and then refocus back into Excel to continue the VBA script.
复制单元格 A1 的内容,聚焦到活动的 Internet Explorer 窗口,发送 TAB 键命令,粘贴从 A1 复制的数据,然后重新聚焦回 Excel 以继续 VBA 脚本。
Is this possible? I couldn't find the right information online and feel like it is possible to do this using Excel VBA.
这可能吗?我在网上找不到正确的信息,觉得可以使用 Excel VBA 来做到这一点。
回答by JMax
Within VBA itself, you cannot literally send keys or master another application. Yet, you can use the Microsoft APIs to simulate behavior in the Office Suite or, in some extend, to Internet Explorer.
在 VBA 本身中,您不能按字面意思发送密钥或掌握另一个应用程序。但是,您可以使用 Microsoft API 来模拟 Office 套件中的行为,或者在某种程度上模拟 Internet Explorer 中的行为。
You can either:
您可以:
- use C#to create a Microsoft Windows level application.
Here are some links: - use VBA and IE APIsto call a webpage and fill an input with the value of your Excel file.
Here are some links:- on Excely.com
- on vba-corner
- 使用C#创建 Microsoft Windows 级应用程序。
以下是一些链接: - 使用 VBA 和IE API调用网页并使用 Excel 文件的值填充输入。
以下是一些链接:
回答by CheeseConQueso
I ended up finding & using this software called AutoHotkey. It's free & open source.
我最终找到并使用了这个名为AutoHotkey 的软件。它是免费和开源的。
It allows you to script macros using a proprietary syntax that's pretty intuitive to use.
它允许您使用非常直观的专有语法编写宏脚本。
The problem I wanted to solve was much easier to execute using AutoHotkey versus Excel VBA.
我想解决的问题使用 AutoHotkey 比 Excel VBA 更容易执行。
The AutoHotkey code for the above pseudo-code:
上述伪代码的 AutoHotkey 代码:
#space::
;Focus to Excel and copy contents of cell A1
WinActivate Microsoft Excel - filename.xls
Send {Ctrldown}{Home}{Ctrlup}
Send ^c
;Focus to Internet Explorer, TAB to the first field, and paste clipboard data into it
WinActivate Google - Windows Internet Explorer
Send {Tab}
Send ^v
;Focus back to Excel
WinActivate Microsof Excel - filename.xls
return
It's not worth going over the specifics of this syntax, but I just pasted it for demonstration purposes.
不值得详细介绍此语法,但我只是出于演示目的粘贴了它。
The program allows for a whole lot more and is worth a closer look if you need to automate tasks that require special mouse & keyboard actions across multiple windows. Normal programming aspects such as variables & loops are supported too.
该程序允许更多功能,如果您需要自动执行需要跨多个窗口进行特殊鼠标和键盘操作的任务,则值得仔细研究。也支持变量和循环等常规编程方面。
There is also more general info about the program and it's potential uses on WikiPedia
还有更多关于该程序的一般信息及其在WikiPedia上的潜在用途