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 bkrike on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

syntax for shell in Excel VBA

Status
Not open for further replies.

patstarks

Technical User
Oct 4, 2009
3
US
Hello,

I have a Replace.vbs script which does this operation to a text file "Test.txt"

-finds a string "orig_name"
-replaces it with a string "new_name"

I save the Replace.vbs and test.txt in the same directory:
C:\Documents and Settings\tops\My Documents

and from the command line (start-> run-> cmd)
I go to that directory and enter:

csript Replace.vbs "Test.txt" "orig_name" "new_name"

and the program works fine

However when I go try to do this from an Excel VBA, I can't figure out the appropriate syntax. I tried:

Sub TextEditor()
ChDir "C:\Documents and Settings\tops\My Documents"
Shell cscript(Replace.vbs, test.txt, orig_name, new_name)
End Sub

I get an error saying:

Compile Error: Argument not Optional

Does anyone know the appropriate syntax to call a vbs script that needs multiple arguments?

Note I have tried several combinations of ampersands, commas, quotations, etc. and no luck.

Regards,
patstarks
 
The syntax should be

x=shell("cscript Replace.vbs, test.txt, orig_name, new_name")



In order to understand recursion, you must first understand recursion.
 
Thanks taupirho but for some reason that did not work when I tried it.
I ended up just running a .bat file from VBA using this syntax and it worked out OK.

Shell ("Replace.bat orig_name new_name")

Since I am new to batch files I was probably not asking the right questions.
Regards,
Starks
 
>The syntax should be


Actually I think it should be:

x = Shell("wscript Replace.vbs test.txt orig_name new_name")

or

Shell "wscript Replace.vbs test.txt orig_name new_name
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top