On Windows 7, I get an error for the following line in my VBScript:
Set wsh = WScript.CreateObject("WScript.Shell")
Error:
Microsoft VBScript runtime error: Wrong number of arguments or invalid property assignment: 'wsh'
Using any name other than wsh
works.
I scoured the web for information, but the pages for reserved keywords do not reference any mention of wsh
.
I run the above script using the cscript command in the CMD processor.
UPDATE AFTER QUESTION WAS ANSWERED:
Declaring the variable as Dim wsh
overrides its keyword status, allowing its use in the script. Came across this information after posting the question, here: http://forums.devshed.com/visual-basic-programming-52/bizzare-finding-username-918597.html
wsh
is a builtin alias for the WScript
object, allowing you to write
wsh.Echo "foo"
wsh.StdErr.WriteLine "bar"
wsh.Quit 42
instead of
WScript.Echo "foo"
WScript.StdErr.WriteLine "bar"
WScript.Quit 42
To my knowledge this isn't covered by the documentation, though.
Edit: Apparently you can work around the issue by defining wsh
as a variable before using it:
Dim wsh
Set wsh = CreateObject("WScript.Shell")
However, note that doing so will completely mask the original identifier, i.e. you won't be able to get the original behavior back without leaving the context in which the variable was defined (which in case of global variables means restarting the interpreter), because you can't un-dim a variable.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With