VBScript and CreateObject issue

3.7k views Asked by At

I'm not really experimented if it is about VBScript but I had the occasion to read many things through forums and actually yesterday I helped someone to checkout why his script wasn't working and found him a solution. So I modified this script locally and did it the way I would and it worked but on that other person side one of the object couldn't be initialized.

The incriminated line is like

    Set WshNet = WScript.CreateObject("WScript.Network")

Another person told to remove the WScript thing and it seems it works on the question asker side.

I first thought it might be linked with the use of Wscript.exe on my side and CScript.exe on that person side (my hypothesis) but I checked this out in the command-line and it worked anyway. So, what I would like to know and understand is why is this happening ? Why does a script calling for CreateObject works with "WScript." but on another system you need to remove that "WScript." to keep it working ? Thank you for your time and answers. ;) Best regards.

1

There are 1 answers

1
Ekkehard.Horner On

The "WScript" in "WScript.CreateObject" or "WScript.Echo" is (the name of an object) provided by the c|wscript.exe scripting host. If you run VBScript under/in other hosts (e.g. ie or mshta), there is no such object.

The language itself has a (different!, see the docs for details) "CreateObject" function, that can be used under/in all VBScripts hosts. So use plain "CreateObject" to be on the safe side.

The WScript in "WScript.Network" is part of the ProgId of a COM object that may be installed on your computer (or not). Those COM objects with a 'first name' of "WScript" are completely different from the WScript object provided by c|wscript.exe.

So my first assumption: The "someone" executed the code under/in ie or mshta and solved the problem by removing the "WScript." from "WScript.CreateObject", that is by falling back to VBScript's own CreateObject.