Calling a .vbs script from Excel VBA - vbscript

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

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.

VB Using "Runas"

I am having a heck of a time getting some syntax correct here.
I have a small VB Script which prompts for credentials and then uses those credentials to run another vb script:
set objShell = WScript.CreateObject("WScript.Shell")
strAdminName = inputBox("What is your username (Domain\Username)")
objShell.Run "runas /user:" & strAdminName & " ""Wscript.exe \\xxx.xx.xxx\dfs\Tumw-IS\Juniper Tools and Utilities\Juniper Removal Tools\delete_folders.vbs"" "
When I run the script everything works, except it can't find the script I am calling. I get an error stating "There is no file extension in "\xxx.xx.xxx\dfs\tumw-is\Juniper"
Obviously the problem is in the fact that there are spaces in the file location and also that I am using a DFS link. When I run this and target a VBS file with no spaces in the location it works fine.
I just can't get the syntax down to handle the DFS link or the spaces in the name. I assume it's pretty straightforward, but I just can't get it to work.
Any ideas?
Thanks
-John
Try this:
objShell.Run "runas /user:" & strAdminName & " ""Wscript.exe \""\\xxx.xx.xxx\dfs\Tumw-IS\Juniper Tools and Utilities\Juniper Removal Tools\delete_folders.vbs\"""""

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.

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.

Wscript.Shell Run doesn't work consistently

I'm trying to run the following bit of code in a vb6 dll:
Dim objWSShell As Object
Set objWSShell = CreateObject("Wscript.Shell")
objWSShell.Run strPath & "test.bat", 0, True
The dll process gets hung up. The batch file will not run, no matter what its contents. I even tried an empty batch file and it still hung up. However, if I try this same piece of code, with this change:
Dim objWSShell As Object
Set objWSShell = CreateObject("Wscript.Shell")
objWSShell.Run "calc", 0, True
It works fine. I can't figure out why exe files work and bat files don't. Any ideas?
You don't need to use the shell scripting stuff, you can make things simpler & use the built in Shell() function:
shell environ$("COMSPEC") & " /C c:\xxx\yyy.bat", vbNormalFocus
Ditto for:
shell "calc", vbNormalFocus
You need to run cmd.exe and pass your BAT file to it.
objWSShell.Run "%COMSPEC% /c " & strPath & "test.bat", 0, True
I had a similar issue where batch files couldn't be run directly from WScript.Shell, but I didn't have access to modify the VBScript. It turns out there was a registry override on the .bat extension.
While using COMSPEC worked for me, deleting the registry key actually fixed more than just the WScript problem.

Resources