VBScript - Reactivate previous window after using inputbox - vbscript

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

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}")

Related

VBScript helper: getting focus on app

Within a frontend (feel), I need to call a batch file which will close the FE (necessary to prevent it from crashing), do some work and call the FE again.
Anyway, when the FE will be called again, it won't have focus for user input.
At this point, I wrote a VBScript, to put at the end of the batch file, after the FE is called, in order to get focus back on it:
Dim bWindowFound
Dim WShell
Set WShell = CreateObject("WScript.Shell")
Do
bWindowFound = WShell.AppActivate("Feel")
If (bWindowFound = -1) Then
WShell.SendKeys ("{ENTER}")
Else
WScript.Sleep 1000
End If
Loop Until bWindowFound = -1
'WScript.Echo "Feel found, quitting ... "
Set WShell = Nothing
WScript.Quit(0)
What happens is that the feel window will blink in the application bar but it won't come in front of all the others (so no input can be provided).
The only solution would be to uncomment the following line at the end of the script:
WScript.Echo "Feel found, quitting ... "
This way it will work: I'll quickly see a pop up window saying "Feel found, quitting ... " and the feel window will come in front of all the others with focus on it.
Unfortunately this is not feasible since I don't want to see any window and, above all, I don't want to have to close it manually!

Save a popup window to text using VBS

I have created a code that will open Internet Explorer, navigate to a site, prompt for credentials and a date range, and then save the data (theoretically) to a text file.
Everything works except for saving the data. When I click their export button, it pops up a new window that is untitled (so I can't focus on the window by name) and when I try to save it to a .txt file it saves the 1st webpage that is still focused, not the popup window.
How do I shift focus to the popup window/save the popup to a text file?
Unfortunately I can't provide much code, but here is the code I am using to save to a text file:
TEXT = objIE.Document.Body.InnerText
Set fso = CreateObject("Scripting.FileSystemObject")
Set ts = fso.OpenTextFile("Banorte.txt", 2, True)
ts.Write TEXT
ts.Close
Also, I don't have an option to download any software to help me with this. I am also not opposed to using a different language if need by, or call a separate script.
Thank you for taking a look!
------EDIT------
Here is some additional code, the site is banorte.com, this is the code that runs after I log in.
And by popup, I mean it opens up a new window in internet explorer, but it is not like a tab, that has a name and could be added to the parent window. It is a standalone.
objIE.Document.getElementsByName("btnEntrar").Item(0).Click
While objIE.Busy
WScript.Sleep 100
Wend
objIE.Document.getElementsByName("ctl00$Contentplaceholder1$btnConsultar").Item(0).Click
WScript.Sleep 5000
objIE.document.parentWindow.execScript "javascript:__doPostBack('ctl00$Contentplaceholder1$grdCuentasCheques$ctl02$ctl00','')"
BEGDATE= InputBox("Please enter the beginning date DD/MM/YYYY:","Please provide additional info.")
objIE.Document.getElementById("ctl00_Contentplaceholder1_ucPeriodoFechas_WucFechaInicial_txtFecha").value = BEGDATE
ENDDATE= InputBox("Please enter the ending date DD/MM/YYYY:","Please provide additional info.")
objIE.Document.getElementById("ctl00_Contentplaceholder1_ucPeriodoFechas_WucFechaFinal_txtFecha").value = ENDDATE
objIE.Document.getElementsByName("ctl00$Contentplaceholder1$btnConsultar").Item(0).Click
WScript.Sleep 2000
Issue is after this command:
objIE.Document.getElementsByName("ctl00$Contentplaceholder1$btnExportarSaldos").Item(0).Click
Do Until objIE.readystate = 4
WScript.Sleep 10
loop
TEXT = objIE.Document.Body.InnerText
Set fso = CreateObject("Scripting.FileSystemObject")
Set ts = fso.OpenTextFile("Banorte.txt", 2, True)
ts.Write TEXT
ts.Close
objIE.Navigate "https://nbem.banorte.com/NBXI/Corporativa/Cerrar.aspx"
ObjIE.quit

vbscript to auto detect open window and close it OR Google Apps Script to control form submit button

In the past I've used the script below to detect if an active window with the title "Remote Desktop" is or isn't active. If it isn't active the script will auto launch or make it active again.
Question: Is there a way to detect if a window is active and auto close it? I'm setting up a kiosk in chrome kiosk mode on a Windows 7 machine for our office. The main page launches a selected Google form in a pop-up window. The form confirmation page has the title "Thank You!" in the title bar. Is there a way for the script to auto detect this window and close it? This would be nice because the user would see that their response was submitted (for a second or two) but if they did not close the window it would not still be open when the next user goes to use the kiosk.
Another option might be if there is a way to use a Google script on the form to program the submit button to close the window. I'm not sure if that's possible.
Option Explicit
'On Error Resume Next
Dim objShell
Set objShell = CreateObject("WScript.Shell")
Do
If (objShell.AppActivate("Window Title Here") = False) Then
objShell.Run "mstsc.exe " & chr(34) & "c:\scripts\Remote Desktop.rdp" & chr(34)
WScript.Sleep 5000
Else
WScript.Sleep 3000
End If
Loop
If the window I need closed is active and then the following script is ran the window will close. It's almost like I need to piece the top and bottom code here together to achieve what I need, but I'm not sure how.
Dim oShell
Set oShell = CreateObject("WScript.Shell")
If oShell.AppActivate("Untitled - Notepad") Then
WScript.Sleep 500
oShell.SendKeys "%{F4}"
End If
I'm trying to find a script that will run on startup and wait for a window with a specific title to open and then close it once it is detected. It would be even better if I could control how long the window remains open once detected, but if I could just get it to close that would suffice.
I think I've found a good solution. I found this post and modified the answer. Does anyone see any issues with this?
' Will loop forever checking for the message every half a second
' When it finds the message it will close the window
Set wshShell = CreateObject("WScript.Shell")
Do
ret = wshShell.AppActivate("Untitled - Notepad")
If ret = True Then
wshShell.SendKeys "%{F4}" 'ALT F4
End If
WScript.Sleep 500
Loop
Set wshShell = CreateObject("WScript.Shell")
Do
ret = wshShell.AppActivate("Untitled - Notepad")
If ret = True Then
wshShell.SendKeys "%{F4}" 'ALT F4
End If
WScript.Sleep 500
Loop
The only trouble i see is the Alt+F4 will sequentially close windows until it will want to shut down windows itself. kinda makes me nervous even though this script will only detect the name you give it.
I tried the script and it works fine but what about using the escape key for certain windows?
The other thing i do not like is this is always running and taking resources. I slowed down the loop so it will at least be paused in the background most of the time..

How can I maximize, restore, or minimize a window with a vb script?

I need to be able to make separte .vbs files that will (when triggered with a keyboard short-cut) will make the active window maximized, minimized, or restored.
How can I do this without downloading and installing (not allowed here) a separate package.
VBScript and Windows Script Host don't provide intrinsic functions for maximizing/minimizing/restoring a window. Without any third-party tools, your only option is to use SendKeys to simulate keyboard the shortcuts of the corresponding commands in a window's system menu.
To maximixe the active window, you can simulate the Alt+SpaceBar, x shortcut:
Set oShell = CreateObject("WScript.Shell")
oShell.SendKeys "% x"
To minimize the active window, use Alt+SpaceBar, n:
Set oShell = CreateObject("WScript.Shell")
oShell.SendKeys "% n"
To restore the active window, use Alt+SpaceBar, r:
Set oShell = CreateObject("WScript.Shell")
oShell.SendKeys "% r"
(Note that this code won't work in non-English Windows versions, where the names of the Maximize/Minimize/Restore commands are localized and therefore have other shortcuts.)
SendKeys was not working in my computer. Spanish native with Spanish and English keyboard. I did this and worked in my code as instruction and worked to maximize my excel window. I put the .Sleep to visually check it.
objExcel.SendKeys"% x"
objExcel.Visible = True
objExcel.SendKeys"% x"
WScript.Sleep 2000
To maximize any window, the following code will work:
Application.SendKeys "%{ }"
Application.Wait (Now + TimeValue("00:00:02"))
Application.SendKeys "x"
Topic is old. But I managed to find language independent solution. SendKeys can basically send any keys to application, including arrow keys and enter key. So we can emulate those actions without particular letters (x,r,n). Here is working example:
Dim oShell : Set oShell = CreateObject("WScript.Shell")
oShell.SendKeys("% {DOWN}{DOWN}{DOWN}{DOWN}{ENTER}") 'Maximize
'...
oShell.SendKeys("% {ENTER}") 'Restore
'...
oShell.SendKeys("% {DOWN}{DOWN}{DOWN}{ENTER}") 'Minimize
This works for me in my Excel macro to maximise an external PDF document by executing the shortcut keys 'Alt+Spacebar+x'.
Mote: '%' represents the Alt key, '( )' represents the spacebar key and 'x' represents the maximise key.
Application.SendKeys "%+( )+(x)", True

Focus moves next tab if user press any key

I tried making a script that should perform the following functions:
runs in background
focus moves to next tab if user press any key
run on windows platform.
I used the following code but its not working :(
Please correct me if I had made a error in code.
Set wshShell = WScript.CreateObject("WScript.Shell")
Dim a
Do
a = WScript.StdIn.Read(1)
wshShell.SendKeys "{TAB}"
Loop

Resources