How to wait for Word Object to complete? - vbscript

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.

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.

Wscript make proccess not run on background or wait while finishes

Scenario: We have some scheduled jobs (Control-M) running many proccesses and some of them execute a .VBS file (with parameters) that reads the configuration from an XML file and sends it to a company's software that interprets it and loads data onto tables.
I need help with the VBS file, that as stated above, only gets the instructions from the XML and send it to the software with these steps (also, logs each step):
Finds the XML;
Creates an object to login the software (with XML parameters);
Dim object
Set object = CreateObject("service.location.id")
Login into the Database (with XML parameters);
object.Login("DATABASE_NAME")
Select which Data base (XML...);
object.SelectDatabase("DATABASE_NAME")
Sends command to start load proccess
object.LoadRepositoryTable(XML)
The problem is that since the default interpreter is wscript, when executing the script, it runs on the background and the Job Scheduler thinks it finished executing and starts the next job.
Executing the script on CMD, if I start it as cscript SCRIPT.vbs it waits for the whole load proccess to finish (it doesn't run on background), and I want to do this when running as wscript - because since there are many jobs, editing how they calls the script is not an option right now. This includes creating an .bat file that calls the SCRIPT.vbs as cscript.
I also tried searching on "how to run another .vbs file inside a VB Script" to run as cscript, but didn't manage to make it work.
What can I do?
Is there a way to make wscript wait for the load?
Is there a way to call a .vbs in the same folder (without sending it as parameter) while passing 2 arguments?
Another option I'm not seeing?
Edit: Here is what I've come to so far, but I'm getting "expected end of statement".
Dim objShell
Dim arg_dir
arg_dir = Wscript.Arguments.Item(0)
Dim arg_xml
arg_xml = Wscript.Arguments.Item(1)
Set objShell = CreateObject("Wscript.Shell")
objShell.Run "cmd /k cscript ""C:\Folder\Scripts\SCRIPT.vbs" &" "& arg_dir &" "& arg_xml"", 1, True
cscript is the solution for your problem. Either have your scheduler run each VBScript with cscript.exe, or (if that isn't possible for some reason) make cscript the default script host by running
wscript.exe //h:cscript
once as an administrator.
The script you have cobbled together won't resolve your problem, even if you fixed the syntax error in the last line, because as long as you run it with wscript.exe it will still return immediately. It doesn't matter that the code in that script is running other scripts synchronously as long as all of that is happening in the background.

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.

Under what conditions does Wscript.Echo show up as popup instead of console

I was writing a vbs script which had an output to the console using the WScript.Echo command to let the user know what iteration the program was at. This worked fine until I was working on it again today when all of a suddent the script is echoing the message as a popup box. I stripped the code down to the bare bones and its still doing it and I am completely baffled.
Option Explicit
Call F
Sub F()
Dim TimeInterval
Dim CycleCount
Dim CycleCounter
TimeInterval=FormatNumber(WScript.Arguments(0),0)
CycleCount=WScript.Arguments(1)
CycleCounter = 0
For CycleCounter = 1 To CycleCount
WScript.Sleep(TimeInterval)
WScript.Echo "Iteration " & CycleCounter & " / " & CycleCount
Next
End Sub
I don't see it being the code that is causing it but rather the environment it is being run in but I'm not sure how I'd go about checking where to alter the settings or how the settings were even altered in the first place.
It will display a popup if you're using Wscript.exe to run your script. It will display in the console if you're using CScript.exe to run your script.
See this (among many other resources on the web): Should we use CScript.exe or WScript.exe to run .vbs files?

VBS gives error 'Expected end of Statement'

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.

Resources