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

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?

Related

invalid window handle with error code : 80070578

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 .

How to run below command from vbscript

I have written vbscript code to set classpath of putty from command prompt
Dim oShell
putty_path="setx path %path%;C:\putty"
Set oShell = WScript.CreateObject ("WScript.Shell")
oShell.run "cmd /K" & putty_path
Set oShell = Nothing
When i execute vbscript code i am getting below error in command prompt
ERROR: Invalid syntax. Default option is not allowed more than '2' time(s).
Type "SETX /?" for usage. Please help
Because it has already worked, so it can't do it a second or subsequent time as it worked the first time. Just ignore.
Plus it's unnecessary what you are doing, just shell to PuTTY (you don't even need to use the extension).
oShell.run "putty"

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.

VB Script to open multiple programs at once

Right im looking for a script that I can click on after I have logged in to open various programs just to save me a bit of time. I have managed to get a script to open one but as a bit of a newbie can someone provide advice.
Dim objShell
Set objShell = WScript.CreateObject( "WScript.Shell" )
objShell.Run """C:\Program Files (x86)\servicecenter\Run\scguiw32.exe "" ""-
express:dvla.servicecenter.fs.fujitsu.com.12680"""
Set objShell = Nothing
You might be overthinking it a bit to use VBScript or Powershell for this job. A batch file will work.
#echo off
start "c:\Program Files\Folder 1\program.exe"
start "c:\Program Files\Folder 2\program.exe" -switch -argument
exit
Dim objShell
Set objShell = WScript.CreateObject( "WScript.Shell" )
objShell.Run("Path to program")
wscript.sleep (100)
objShell.Run("Path to program")
wscript.sleep (100)
wscript.quit
I do not have scguiw32.exe, so I created simple script which opens file in notepad and in word.
Dim objShell
Set objShell = WScript.CreateObject( "WScript.Shell" )
objShell.Run "C:\Windows\notepad.exe c:\dump.txt"
objShell.Run """C:\Program Files (x86)\Microsoft Office\Office14\winword.exe"" c:\dump.txt"
Set objShell = Nothing
BTW Instead of vbscript you can use now powershell, and powershell script is much more easy to understand. For example above one will be: Create run.ps1 with content
& 'C:\Program Files (x86)\Microsoft Office\Office14\WINWORD.EXE' c:\dump.txt
notepad.exe C:\dump.txt
Click it right-click and choose Run with Powershell
Here is how to use vbscript to create an array of programs you want to run and then execute each one.
'---Declare Variables
Dim objShell, strprogram1, strProgram2, colprograms, item
'---Create Scripting Shell Object
Set objShell = CreateObject("WScript.Shell")
'---Create Program Variables
strProgram1 = """C:\Program Files (x86)\servicecenter\Run\scguiw32.exe"" ""-express:dvla.servicecenter.fs.fujitsu.com.12680"""
strProgram2 = "C:\Windows\notepad.exe C:\Dump.txt"
'---Add Variables to an Array
colPrograms = Array(strProgram1,strProgram2)
'---Run each program in the array once
For Each item In colprograms
objShell.Run item
Next
WScript.Echo "Done."

how do I execute several programs consecutively in vbs

I have a couple of applications that I would like to execute one following the other.
how do I do this?
I tried this but the second task never executed.
on error resume next
Set WshShell = CreateObject("WScript.Shell")
WshShell.Run """C:\Program Files\my folder\do task1.exe.vbs"""
WshShell.Run """C:\Program Files\my folder\do task2.exe.vbs"""
msgbox "Finished tasks"
update:
notes found on WshShell.Run click here
What you are missing (per ISDi's answer) is the third parameter of Run, which tells it to not wait for the program to quit (false), before continuing code exection.
Try (If you want to put your code in a subroutine, which if good coding practice for repeat activities):
'Place all of the following in a .vbs file
Sub RunApplication(ByVal sFile)
Dim WShell : Set WShell = CreateObject("WScript.Shell")
WShell.Run Chr(34) & sFile & Chr(34), 8, false
End Sub
'Executing apps.
RunApplication "C:\Program Files\my folder\task1.exe"
RunApplication "C:\Program Files\my folder\task2.exe"
The Run method of WScript.shell has an optional parameter that can halt execution of the script until the Run method returns.
Try:
WshShell.Run("""C:\YourPathTo\task1.exe""", 1, true)
The third parameter, true in the line above tells the interpreter to wait until this task exits before continuing to the next line of the script.
-isdi-

Resources