0

For my job I often need to enter standard texts in an application to show which actions have been taken. At a previous job I have been using a VBScript that I found that worked perfectly. What it does is open up an inputbox, you enter a number and the script runs the corresponding subroutine entering the text you requested. Below you can find an example script of how it works. In the past I used it in Windows XP, which worked perfectly:

  • I'm in my application, ready to input text.
  • I use a shortcut key to start up the VBScript, say CTRL-ALT-T
  • The inputbox opens
  • I enter a number
  • The inputbox closes and Windows XP automatically goes back to my application
  • The text is being put in the application

But Windows 7 has a slightly different task behaviour. When a window closes in W7, the previous window appears on screen but is NOT actively selected. As a result, the text is not being put in that window.

Is there any way to make the window actively selected?

I'm interested in a solution in VBScript form. But if someone happens to know a solution to change Windows 7's behaviour so it behaves like Windows XP concerning this issue, that would be interesting as well. I find the old behaviour much more convenient in general.

Here's a short example of the script:

'-----------------------------
'VBScript for inputting text
'-----------------------------

Option Explicit
Dim oFS, oWS, oWN, Shell

Set oWS = WScript.CreateObject("WScript.Shell")
Set oWN = WScript.CreateObject("WScript.Network")
Set oFS = WScript.CreateObject("Scripting.FileSystemObject")
Set Shell = CreateObject("WScript.Shell")
'-------
' Menu
'-------
Select Case InputBox ( _
"Enter type of comment, then press enter or click Ok..." & vbCrlf & vbCrlf & _
" [1] Text 1" & vbCrlf & _
" [2] Text 2", _
"Menu")

Case "1"
Call sub1()
Case "2"
Call sub2()

Case Else
WScript.Echo "You entered an invalid menu choice!"

End Select

Call CleanUp

'------------
' Subroutines
'------------

'------------------
Sub CleanUp()
Set oWS = Nothing
Set oWN = Nothing
Set oFS = Nothing
WScript.Quit
End Sub

'------------------
Sub sub1()

WScript.Sleep 500
Shell.SendKeys "Standard text 1{ENTER}"

End Sub

'------------------
Sub sub2()

WScript.Sleep 500
Shell.SendKeys "Standard text 2{ENTER}"

End Sub
Frank359
  • 1
  • 2
  • you could perhaps combine http://stackoverflow.com/questions/26341753/how-to-find-the-window-title-of-activeforeground-window-using-window-script-ho (before showing your input box) with http://stackoverflow.com/questions/35205587/activating-bring-to-foreground-a-specific-window-with-vbscript (after showing the input box) – Keith Hall Jan 23 '17 at 06:46
  • One of the solutions mentioned there is creating a COM, which unfortunately is not an option for me. Another option of using the name of the window to reactivate it is also not possible, because I have several windows with the exact same name. – Frank359 Jan 26 '17 at 15:44

1 Answers1

0

I have found a simple solution which I have been using for a few days now without any problems. I added below code right after the beginning of each sub:

Shell.SendKeys("%{ESC}")
Frank359
  • 1
  • 2