我可以在 Excel 2007 中使用 VBA 命令将焦点转移到另一个窗口吗?

发布于 2025-01-01 18:51:03 字数 267 浏览 3 评论 0原文

我想从 Excel 2007 运行一个 VBA 脚本,该脚本可以将焦点切换到另一个活动窗口 (ALT+TAB),将命令发送到该窗口,然后将焦点返回到 Excel 并继续在 VBA 中执行命令。

例如:

复制单元格 A1 的内容,聚焦到活动的 Internet Explorer 窗口,发送 TAB 键命令,粘贴从 A1 复制的数据,然后重新聚焦回 Excel 以继续执行 VBA 脚本。

这可能吗?我在网上找不到正确的信息,感觉可以使用 Excel VBA 来完成此操作。

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.

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.

Is this possible? I couldn't find the right information online and feel like it is possible to do this using Excel VBA.

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

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

发布评论

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

评论(2

左耳近心 2025-01-08 18:51:03

在 VBA 本身内,您无法真正发送密钥或掌握另一个应用程序。然而,您可以使用 Microsoft API 来模拟 Office 套件中的行为,或者在某种程度上模拟 Internet Explorer 中的行为。

您可以:

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.

You can either:

殊姿 2025-01-08 18:51:03

我最终找到了&使用名为 AutoHotkey 的软件。它是免费的&开源。

它允许您使用使用起来非常直观的专有语法来编写宏脚本。

我想要解决的问题使用 AutoHotkey 比 Excel VBA 更容易执行。

上述伪代码的 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

不值得详细介绍此语法的细节,但我只是将其粘贴用于演示目的。

该程序允许执行更多操作,如果您需要自动执行需要特殊鼠标和鼠标的任务,则值得仔细研究。跨多个窗口的键盘操作。正常的编程方面,例如变量和变量也支持循环。

WikiPedia 上还有有关该程序及其潜在用途的更多一般信息

I ended up finding & using this software called AutoHotkey. It's free & open source.

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.

The AutoHotkey code for the above pseudo-code:

#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

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