invalid window handle with error code : 80070578 - vbscript

I have written below vb script to find an window and bring it to focus
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 WScript.Arguments(0)
I am calling this vb script from java code :
Runtime.getRuntime().exec("cmd.exe /c activeWindow.vbs mywindowname");
Now sometimes it is throwing this below exception :
This is happening randomly.

Syntax for AppActivate :
objShell.AppActivate strApplicationTitle
Below code is working directly by passing title name
Dim oShell
Set oShell = CreateObject("WScript.Shell")
oShell.AppActivate "Calculator"
Or Passing as argument "WScript.Arguments(0)". Save this code in VBS file
Dim oShell
Set oShell = CreateObject("WScript.Shell")
oShell.AppActivate WScript.Arguments(0)
WScript.Quit 0
from cmd type wscript Sample.vbs Calculator
Code is working smoothly and Calculator screen is activating .

Related

Declare VBScript inside of VBScript (Running multiple .VBS at a time)

I need to run multiple .vbs at a time. This is the only way I was able to find online:
Dim objShell
Set objShell = Wscript.CreateObject("WScript.Shell")
objShell.Run "1.vbs"
objShell.Run "2.vbs"
objShell.Run "3.vbs"
Set objShell = Nothing
To do that I'd have create 1.vbs, 2.vbs and 3.vbs separately. Is there a way to input them all as part of one .vbs file? Something like
Dim objShell
Set objShell = Wscript.CreateObject("WScript.Shell")
Dim vbs1 as vbscript
Dim vbs2 as vbscript
Dim vbs3 as vbscript
Set vbs1 =
"Set objExcel = CreateObject("Excel.Application")
objExcel.Application.Run "'C:\Users\test1.xlsm'!Module1.refresh"
objExcel.DisplayAlerts = False
objExcel.Application.Quit
Set objExcel = Nothing"
Set vbs2 = ' whatever code
Set vbs3 = ' whatever code
objShell.Run vbs1
objShell.Run vbs2
objShell.Run vbs3
Set objShell = Nothing
The purpose of this:
I have ~50 excel reports with connections to SQL that need to be updated every day.
To do that I've created a macro and added it to each of them. The macro refreshes connections/queries > refreshes pivot tables > removes the connections/queries > saves as a macro-free workbook in a specified location.
I wrote .vbs scripts for each report. It just runs that macro in every Excel workbook.
Instead of running every .vbs separately, I've created one main .vbs that references all prior created .vbs to run them at the same time.
My question is if there's a way to do that without creating 50 separate .vbs files.

Restart vb script

I'm trying to make a Vb script that restarts another vbs script the problem is I'm new to this and I don't know how to do this, someone suggested using WshShell I have tried a few websites on how to use it but nothing. Here is what I've got,
Dim WshShell, oExec
Set WshShell = CreateObject("WScript.Shell")
Set oExec = WshShell.Exec("Test_To_Block.vbs")
Do
If NOT WshShell.Status = 1 then
WScript.Exec("Test_To_Block.vbs")
End If
WScript.Sleep(100)
Loop
Thanks,
Regards,
A Viper
Yes, you can use Exec method to run another VB Script, but you are likely to get a console window flash.
Dim WshShell
Set WshShell = WScript.CreateObject("WScript.Shell")
WshShell.Exec("CMD /C Test_To_Block.vbs")
Refer to SS64 site to learn about VB Script basics.

How to get keyboard input of "CTRL L" command in VBScript

Is this the correct way of taking input from the keyboard (CTRL+L)?
dim WshShell
set WshShell = CreateObject("WScript.Shell")
wait(2)
WshShell.SendKeys("^108")
wait(2)
or
dim WshShell
set WshShell = CreateObject("WScript.Shell")
wait(2)
WshShell.SendKeys("^l")
wait(2)
The correct way to send a CTRL-L using SendKeys is like this:-
WshShell.SendKeys("^l")
Here is the MSDN reference for the SendKeys method to get more information:
https://msdn.microsoft.com/en-us/library/8c6yea83(v=vs.84).aspx

Run external command silently

I am trying to run cmd code from vbscript (vbs file) silently.
I have tried this, but it doesnt hide the cmd window.
Dim oShell
Set oShell = WScript.CreateObject ("WScript.Shell")
oShell.run "cmd.exe /K ping example.org"
Set oShell = Nothing
What is the correct way to do that ?
Dim oShell
Set oShell = WScript.CreateObject ("WScript.Shell")
oShell.run "cmd.exe ping example.org",0,True
Set oShell = Nothing
As per the MSDN for .Run you can use the optional parameter for intWindowStyle, which will hide most windows from the screen, by setting it to 0. The True is to tell the operation to wait until completion before completing the script. That is of course optional.
If you hide the window you need to remove /K or else the script will never complete.

why WScript.CreateObject ("WScript.Shell") should crash?

Do you think of any reason that this line should crash:
Set oShell = WScript.CreateObject ("WScript.Shell")
... only if I launch the script from InstallShieldExpress as a custom-action.
MsgBox "before create ObjectShell"
Set oShell = WScript.CreateObject ("WScript.Shell")
MsgBox "after create ObjectShell"
I never see the "after create ObjectShell" message ;-(
and if I simply launch the script by double-clicking on the script file in a windows explorer, of course everything is ok.
It may be that the global WScript object isn't available in the InstallShield environment. You can check this using a script like this:
MsgBox Not IsEmpty(WScript) ' True if WScript is defined, False if it's undefined
If WScript is undefined, try using CreateObject("WScript.Shell") instead. See also What is the difference between CreateObject and Wscript.CreateObject?

Resources