3

I have a small script that brings an open program (Notepad) into focus, and then waits a second before sending the program the function CTRL S. This script works 50% of the time, while the rest of the time the script makes an error and actually sends a capital S letter right into the .txt file I am editing instead of sending CTRL S (which is command to save).

Below is the content of the script:

' Create WScript Shell Object to access filesystem.
Set WshShell = WScript.CreateObject("WScript.Shell")

' Select, or bring Focus to a window named `NOTEPAD`
WshShell.AppActivate "Notepad"

WScript.Sleep 1000

WshShell.SendKeys "^s"

Can anyone tell me how this error is occurring? I thought perhaps it was becuase I had CAPSLOCK on, but that was not the case.

Running Windows & 64 bit.

4
  • Does the error occur only if you are actively using Notepad at the time? If Notepad is open, but you don't touch the keypad or mouse, does the error still occur sometimes? – Rich Holton Apr 22 '17 at 11:40
  • @RichHolton yes. Notepad is always open when this error occurs because the script is for notepad. The error always occurs when I dont touch keyboard. I am using joystick to activate the script – Kalamalka Kid Apr 22 '17 at 18:26
  • 2
    I've seen similar issues (not notepad, but another program where I need to launch it and send a single keypress) when I didn't wait long enough for the application to stabilize before sending the press (the ctrl went somewhere other than notepad). I've fixed this by just waiting longer. You'll note that Rahul's answer below waits 1500ms (an extra 1/2 second), which could also account for their success. – Ruscal Apr 25 '17 at 15:22
  • 1
    KalamalkaKid - Since you say it already works half of the time, try adding three additional lines beneath the WshShell.AppActivate "Notepad" in your VBS code and add that same code on the next three lines so you end up with four total lines one right after the other as WshShell.AppActivate "Notepad" without any sleep functions in between and then do the 1 second sleep followed by the Ctrl+S. Test to see if this works more than just half the time or all the time, and if so and you'd like, I'll be happy to add an answer. – IT Thug Ninja Apr 26 '17 at 2:56
1

Okay, so. I know that this extreme hackery makes no sense whatever, but it worked every time for me, so you should probably try it:

' Create WScript Shell Object to access filesystem.
Set WshShell = WScript.CreateObject("WScript.Shell")

' Select, or bring Focus to a window named `NOTEPAD`
WshShell.AppActivate "Notepad"
WScript.Sleep 1500
' Minimize
WshShell.SendKeys "% n"
WshShell.AppActivate "Notepad"
WScript.Sleep 1500
' Maximize
WshShell.SendKeys "% x"
WshShell.AppActivate "Notepad"
WScript.Sleep 1500
' Restore
WshShell.SendKeys "% r"
WshShell.AppActivate "Notepad"
WScript.Sleep 1500
' MAGIC!
WshShell.SendKeys "^s"

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.