Is wsh a reserved word in VBScript?
Asked Answered
I

1

6

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

Ides answered 5/1, 2017 at 15:31 Comment(5)
Yes. wsh is a builtin alias for the WScript object.Entertaining
@AnsgarWiechers I can't find that documented anywhere. This is a good question. I've never come across wsh before in all my years.Legislator
@Legislator I don't think it's documented anywhere (at least I haven't seen any documentation menioning it). I think I learned about it in the visualbasicscript.com forums a couple years back.Entertaining
@AnsgarWiechers -- well, it's documented here, now, to some degree then. Although it could use an official answer.Legislator
@Legislator Your request is my command. ;)Entertaining
E
9

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.

Entertaining answered 5/1, 2017 at 15:46 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.