Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Westi on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Invoke VBScript from VBA

Status
Not open for further replies.

Argh2

Programmer
Sep 21, 2006
12
US
How do you call a vbscript (.vbs file) from VBA?

I tried shell, but it causes an "Invalid procedure call or argument" error.

Code:
Sub test1()
    [COLOR=green]'This line works fine[/color]
    Shell "C:\Windows\system32\Calc.exe", 1
    
    [COLOR=green]'This line causes an invalid procedure call or argument error[/color]
    Shell "D:\Temp\TestScript1.vbs", 1
End Sub

Thanks
-Argh2
 
And what about this ?
CreateObject("WScript.Shell").Run "D:\Temp\TestScript1.vbs", 1

Hope This Helps, PH.
Want to get great answers to your Tek-Tips questions? Have a look at FAQ219-2884 or FAQ181-2886
 
Thanks, PH,

Code:
CreateObject("WScript.Shell").Run "D:\Temp\TestScript1.vbs", 1

causes an Method 'Run' of object 'IWshShell3' failed error.

-Argh2
 
What happen when you doubleclick TestScript1.vbs in an explorer window of D:\Temp ?

Hope This Helps, PH.
Want to get great answers to your Tek-Tips questions? Have a look at FAQ219-2884 or FAQ181-2886
 

PH,

Double-clicking the .vbs script works fine.
The .vbs is just a test:

Code:
Msgbox "This .vbs script is called from VBA"

-Argh2
 
No space in the script full pathname ?

Hope This Helps, PH.
Want to get great answers to your Tek-Tips questions? Have a look at FAQ219-2884 or FAQ181-2886
 

PHV,
BINGO!

If I locate the .vbs script at D:\Temp it works fine, but my actual path is D:\Documents and Settings\..., so based on your last post, I've revised the code to:

Code:
Sub test1()
    'This line works fine
    Shell "C:\Windows\system32\Calc.exe", 1
    
    'This line now works fine too
     CreateObject ("WScript.Shell").Run quote("D:\Documents and Settings\Argh2\TestScript1.vbs")
End Sub


Function quote(str As String) As String
Const qu = """"
quote = qu & str & qu
End Function

Thanks for your help!
-Argh2
 
Or simpler:
CreateObject ("WScript.Shell").Run Chr(34) & "D:\Documents and Settings\Argh2\TestScript1.vbs" & Chr(34)

Hope This Helps, PH.
Want to get great answers to your Tek-Tips questions? Have a look at FAQ219-2884 or FAQ181-2886
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top