VBS gives error 'Expected end of Statement' - vbscript

Hi I'm trying to run this script but it's giving me 'Exepected end of statement.
wscript.exe "C:\test.vbs" "your_file.bat"
I'm putting the above in run.vbs, and I'm trying execute run.vbs from withing windows by double clicking the file. I get 'Expected end of statement' error
in the invis.vbs there is :
CreateObject("Wscript.Shell").Run """" & WScript.Arguments(0) & """", 0, False
Earlier I got this solution from : Running Batch File in background when windows boots up
Please advise.

The wscript.exe "c:\test.vbs" "your_file.bat" you can't put in run.vbs. It's batch script. Put that in a run.bat....
or
Modify run.vbs to look like:
Set objShell = CreateObject("WScript.Shell")
objShell.run("wscript C:\test.vbs your_file.bat")

This
wscript.exe "C:\test.vbs" "your_file.bat"
is meant for textual input in the command prompt/console/dos box window. If you put it into a .VBS file, you (deserve and) get a syntax error. This:
CreateObject("Wscript.Shell").Run """" & WScript.Arguments(0) & """", 0, False
looks like valid VBScript code (whether in invis.vbs or test.vbs).
Trying to run a .VBS by double click is a bad idea if you plan to pass parameters to the script.
So you should start afresh and think about/describe clearly what you want to achieve.

Related

Failure to run VBS script with task scheduler

we have this vbs script we use to update certain documents with SyncToy.
This is the script as it is currently written:
'--------------------------------------------------
Set oShell = CreateObject("WScript.Shell")
sFilePath = chr(34) & "C:\Program Files\SyncToy 2.1\SyncToyCmd.exe" &
chr(34) & "-R"
iRC = oShell.Run(sFilePath, 0, True)
' Return with the same errorlevel as the batch file had
Wscript.Quit iRC
'---------------------------------------------------
I didn't write this script, and I have very little experience with scripting.
I have a task set up in task scheduler that runs this script anytime the device connects to a network. The script should run SyncToy and then synchronize the folder pair that is set up. I have tried running the script through command prompt with the cscript command but nothing happens as far as I can tell. At least the folders aren't syncing.
The script is running on a Windows 10 pro tablet
I have verified that the task is indeed running when it is supposed to. I'm just not sure if it is an issue with the way the script is written or if the task settings need to be changed. Is there anything wrong with the script as far as you can tell?
I was unsure whether to post this here or over in serverfault. If this doesn't belong here please move the question over to serverfault
Update: I've verified that this isn't a problem with the script. This problem apparently arose only after the update from SyncToy 2.0 to 2.1.
Thanks Guys.
There is a error with the sFilePath lines.
First, I don't know if this was originaly on a single line but it should (or add "_" before changing line).
Then, this (...)& >"-R" would not work. The ">" symbole is outside the quotes and generate a error.
If you want to execute this command: "C:\Program Files\SyncToy 2.1\SyncToyCmd.exe" -R,
this is the way to do this:
sFilePath = chr(34) & "C:\Program Files\SyncToy 2.1\SyncToyCmd.exe" & chr(34) & " -R"
You can also add msgbox sFilePath to show a popup with the value of sFilePath.
To test/run the script, you just need to double-click on it.

How to wait for Word Object to complete?

I'm trying to run a VBScript to do some operations within a Word file (heavy).
Set objWord = CreateObject("Word.Application")
Set objDoc = objWord.Documents.Open(fileName)
objWord.Visible = False
' Do heavy work here.
' Do heavy work here.
objDoc.SaveAs(file.ParentFolder & "/" & theBaseName & " _compiled")
When this script is called from the command line, there is no way to tell when it has completed as it runs "asynchronously".
How do I wait for process completion?
You seem to be running the script with wscript.exe. Use cscript.exe to run VBScripts synchronously from a console or batch script.
cscript.exe //NoLogo "C:\path\to\your.vbs"
Try WScript.Echo Now() at the top and bottom of the script; you'll see the code runs synchronously. When you get the second message box, instead of clicking OK, check for the file you're saving as; it will be present.

Running DOS commands from Vbscript .Help needed with HTA

I have a vbscript that calls another from within itself. It works fine wen i run it from command line with cscript. For example the master vb script has a code such as
cmd /k cd "Path having the vbs" && cscript "slave_temp.vbs"
Now i have an HTA where the master vbs resides when i click on a button on the HTA this command is executed to call the slave.vbs
This is the command i use to call the slave.vbs
Set objShell = CreateObject("WScript.Shell")
runpath = "cmd.exe /K"& Chr(32) &"cd " & Chr(34)&final_path&Chr(34)&Chr(32)&Chr(38)&Chr(38)& Chr(32)& "cscript " &Chr(34)&Name1 &"_temp.vbs"&Chr(34)
objShell.Run runpath
Here final_path has the path name and Name1 has the file name of the slave.
I tried checking out the runpath value using Msgbox and it seemed all fine. But im dont understand why this doesnt work.
On a Final note the slave.vbs has SAP GUi automation code. and it throws this error when i use HTA to trigger
C:\path of slave\slave_temp.vbs(2, 4) (null): Invalid syntax
But the file executes properly when the slave_temp.vbs is executed separately. What can be the problem here.? Does this mean SAP objects dont have support wen run from HTA ?...
Help is much appreciated.
The following are the first few lines of the slave_temp.vbs having the SAP automation code
If Not IsObject(application) Then
Set SapGuiAuto = GetObject("SAPGUI")
Set application = SapGuiAuto.GetScriptingEngine
End If
If Not IsObject(connection) Then
Set connection = application.Children(0)
End If
If Not IsObject(session) Then
Set session = connection.Children(0)
End If
If IsObject(WScript) Then
WScript.ConnectObject session, "on"
WScript.ConnectObject application, "on"
End If
Sorry for the Long question. But i think the issue is when calling SAPGUI object from HTA. Kindly help me tackle this.

Calling a .vbs script from Excel VBA

Is it possible to call a .vbs script from a VBA code whenever needed? If possible then can you give me a sample code of how to do so?
To run a file:
Shell "wscript c:\null\a.vbs", vbNormalFocus
replacing wscript with cscript if the VBS wants to use the console.
Or you can add a reference to the Microsoft Script Control and interact with the VBScript runtime directly to execute VBS code, procedures etc;
Dim scr As ScriptControl: Set scr = New ScriptControl
scr.Language = "VBScript"
scr.AddCode "sub T: msgbox ""All Hail Cthulhu"": end sub"
scr.Run "T"
I only want to add to Alex' answer, that in some environments the object must be created in the following way:
set scr = CreateObject("MSScriptControl.ScriptControl")
If Alex adds this to his answer, I will delete this one.
Try somthing like this
ChDir ThisWorkbook.Path
Shell "wscript " & ThisWorkbook.Path & "\your.vbs", vbNormalFocus
It helped me.
You may want to use """ if your path name contains a - For me the following solved this issue:
Shell "cscript """ & ActiveWorkbook.Path & """\your.vbs", vbNormalFocus

Get full path of VBScript script file when WScript not available

I'd like to get the full path to the script file I am executing. A quick google search shows me that WScript.ScriptFullName will do the trick. The problem is that I am executing this script outside of the Windows Script Host (I'm using ScriptUnit) and so if I try using that code I get the error:
Variable is undefined: 'wscript'
How do I get the full path to the current script file if I can't use WScript?
Can't you shell out, start a small script with cscript and pass the value to the parentscript throug an environmentvariable ? Can't test this since i don't have or use ScriptUnit.
Obviously the following would not work sincve you don't have Wscript with you but has ScriptUnit a similar feature to read environmentvars ?
Set WshShell = WScript.CreateObject("WScript.Shell")
WshShell.Run "CScript.exe " & """" & ScriptName & """" 'this would set your environmentvar scriptpath
Set objProcessEnv = WshShell.Environment("Process")
path = objProcessEnv("scriptpath")
Or start the script from a batch or other script, record the path in a text-file and read the contents of that file in your mainscript.

Resources