How to mock the background window to active state using VB script?
There is some keycode command I need to send to some background window(I known the related VB function), but it will only work when the window is active. If the window is really active, I cant do anything in the foreground.
So is it there anyway to mock the window to active state?
Yes there is, see this example
Dim oShell
Set oShell = CreateObject("WScript.Shell")
'bring the window to front
'title must be exactly what you see in the titlebar of the window
oShell.AppActivate "title of your window"
WScript.Sleep 500 'give the window the time to activate, time is in miliseconds
oShell.SendKeys "{ENTER}" 'you can send keystrokes to this window
Set oShell = nothing
if you want to minimize that window you can send keystrokes to do that, but this is language dependent so first try them manually. The following sends the keystrokes Alt (%) space and N from miNimize. Try in your window to push the Alt-Space keys to get the menu that controls this
oShell.SendKeys "(% )N"
Related
This code works great. It opens the site in the chrome tab:
Set WshShell = WScript.CreateObject("WScript.Shell")
Return = WshShell.Run("chrome.exe stackoverflow.com " & WScript.ScriptFullName, 0, false)
But is it possible to open the same browser tab behind all other running programs?
What I mean saying behind. Chrome tab is behind the exel window.
From Help
Runs a program in a new process.
object.Run(strCommand, [intWindowStyle], [bWaitOnReturn])
Arguments
object WshShell object.
strCommand String value indicating the command line you want to run. You must include any parameters you want to pass to the
executable file.
intWindowStyle
Optional. Integer value indicating the appearance of the program's
window. Note that not all programs make use of this information.
bWaitOnReturn
Optional. Boolean value indicating whether the script should wait for
the program to finish executing before continuing to the next
statement in your script. If set to true, script execution halts until
the program finishes, and Run returns any error code returned by the
program. If set to false (the default), the Run method returns
immediately after starting the program, automatically returning 0 (not
to be interpreted as an error code).
Remarks
The Run method returns an integer. The Run method starts a program
running in a new Windows process. You can have your script wait for
the program to finish execution before continuing. This allows you to
run scripts and programs synchronously. Environment variables within
the argument strCommand are automatically expanded. If a file type
has been properly registered to a particular program, calling run on a
file of that type executes the program. For example, if Word is
installed on your computer system, calling Run on a *.doc file starts
Word and loads the document. The following table lists the available
settings for intWindowStyle.
intWindowStyle Description
0
Hides the window and activates another window.
1
Activates and displays a window. If the window is minimized or maximized, the system restores it to its original size and position. An application should specify this flag when displaying the window for the first time.
2
Activates the window and displays it as a minimized window.
3
Activates the window and displays it as a maximized window.
4
Displays a window in its most recent size and position. The active window remains active.
5
Activates the window and displays it in its current size and position.
6
Minimizes the specified window and activates the next top-level window in the Z order.
7
Displays the window as a minimized window. The active window remains active.
8
Displays the window in its current state. The active window remains active.
9
Activates and displays the window. If the window is minimized or maximized, the system restores it to its original size and position. An application should specify this flag when restoring a minimized window.
10
Sets the show-state based on the state of the program that started the application.
The following VBScript code opens a copy of the currently running
script with Notepad.
Set WshShell = WScript.CreateObject("WScript.Shell")
WshShell.Run "%windir%\notepad " & WScript.ScriptFullName
The following VBScript code does the same thing, except it specifies
the window type, waits for Notepad to be shut down by the user, and
saves the error code returned from Notepad when it is shut down.
Set WshShell = WScript.CreateObject("WScript.Shell")
Return = WshShell.Run("notepad " & WScript.ScriptFullName, 1, true)
The following VBScript code opens a command window, changes to the
path to C:\ , and executes the DIR command.
Dim oShell
Set oShell = WScript.CreateObject ("WSCript.shell")
oShell.run "cmd /K CD C:\ & Dir"
Set oShell = Nothing
Applies To:
WshShell Object
This question already has an answer here:
How to mock the background window to active state using VB script?
(1 answer)
Closed 5 years ago.
I have written this script and of course every 10 seconds the 'my program' window is activated to send the F9 key. However, what I'd like to do is send the F9 key every 10s to the 'my program' window without actually activating it as it interupts work on other windows/browsers. Will need it to run continuously during my day as a background process ideally. (the F9 key refreshes the screen)
Option Explicit
Dim WshShell
Set WshShell = WScript.CreateObject("WScript.Shell")
Do
WshShell.AppActivate "my program"
WshShell.SendKeys "{F9}"
WScript.Sleep (1000 * 10)
Loop
As Alex K. mentioned, SendKeys can only send keystrokes to the foreground window, so what you're asking simply is not possible with VBScript.
I wrote this little vbs script to press the left arrow key in my Chrome browser:
set WshShell = WScript.CreateObject("WScript.Shell")
WshShell.AppActivate "Google Chrome"
WshShell.SendKeys "{LEFT}"
But it simulates a keypress with instant releases. Is there also a way to parse the pressed key over a longer time?
(I know that this would be easy to handle in Javascript by the Keypress even, but I am trying to learn vbs.)
The WshShell object does not provide a way to send KeyUp and KeyDown events. To closest you can get to what you want to do is by doing repetitions of the same key. This can be done by putting the .SendKeys in a loop or by putting a number after the key within the braces.
set WshShell = WScript.CreateObject("WScript.Shell")
WshShell.AppActivate "Google Chrome"
WshShell.SendKeys "{LEFT 40}"
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..
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