I’m running a vbs file using a keyboard shortcut. Although the code runs fine, the problem with vbs shortcuts is that the window in the foreground loses its focus as soon as you press the keyboard shortcut. (You can try it for yourself by placing an empty vbs file somewhere, a shortcut to the file in e.g. the start menu folder, a keyboard shortcut assigned to the shortcut and pressing the keyboard shortcut.)
I found that by using ALT+TAB, I could get the foreground window to regain focus. However, I’m unable to repeat this functionality in VBA. Apparently, ShellObject.SendKeys("%{TAB}") doesn’t work…
Is there any way to achieve the functionality of ALT+TAB in VBA? Thanks in advance.
EDIT
In the meanwhile, I made a switch to AutoIt to see if it could get me further. This is what I got:
ControlFocus("[CLASS:CabinetWClass]", "", "[CLASS:DirectUIHWND]")
I noticed that selecting the explorer window (i.e. CabinetWClass), is in some cases not enough. That’s why I’m setting focus on the control that actually contains the files/folders.
It’s working very well, but I’m still hoping for a VBA solution 🙂
Can you use the Windows APIs to bring it back to the top? This works for me when run in Excel to bring the notepad to the top, so it should work. You would have to substitute the name of your window in the FindWindow call.
Note: You should be really carefull with some of the flags because you’ll get some weird behaviour if you do some wrong combinations of them.