CheeseConQueso
CheeseConQueso

Reputation: 6041

Can I focus to another window with a VBA command in Excel 2007?

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.

Upvotes: 0

Views: 11256

Answers (2)

CheeseConQueso
CheeseConQueso

Reputation: 6041

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

Upvotes: 0

JMax
JMax

Reputation: 26591

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:

Upvotes: 2

Related Questions