Symptoms
WScript methods and properties cannot be created in the context of a Visual Basic object because they are part of the Windows Script Host (WSH) object model and must be hosted within a WScript object, which can only run in a WSH script.
Resolution
The WScript object is often confused with the automation objects in WSH, such as WScript.Shell and WScript.Network. These automation objects can exist outside of a WSH script, but they must be created in the ObjectContext of the hosting environment (in other words, Server.CreateObject for Microsoft Active Server Pages [ASP] or WScript.CreateObject for WSH or simply CreateObject for Microsoft Visual Basic). Take note of the differences in the following examples. WSH and ASP have intrinsic methods and properties that are not available outside of their respective environments (WSH – Wscript.Echo, ASP – Response.Write).
WSH Sample
Set objWSH = WScript.CreateObject(“WScript.Network”)WScript.Echo objWSH.UserdomainWScript.Echo objWSH.Username
ASP Sample
<%Set objWSH = Server.CreateObject(“WScript.Network”)Response.Write objWSH.UserdomainResponse.Write objWSH.Username%>
Visual Basic Sample
Set objWSH = CreateObject(“WScript.Network”)MsgBox objWSH.UserdomainMsgBox objWSH.Username
Unlike the examples above, which demonstrate WScript automation objects, the following example illustrates a WScript method that cannot be called by an ObjectContext.CreateObject method (note that WScript is not instantiated prior to calling WScript.Sleep and can only be used in a WSH script):
WScript.Sleep 1000objWSH = WScript.VersionobjWSH = WScript.ScriptFullName The following properties and methods are dependent on WSH to be running and will cause an error if they are accessed outside of the context of WSH:PropertiesApplicationArgumentsFullNameNamePathScriptFullNameScriptNameVersionMethodsEcho (for Visual Basic applications, use the MsgBox function or Debug.Print)Sleep (for Visual Basic applications, use the Sleep() Win32 API or SetWaitbleTimer() Win32 API [see "References"]).Popup (for Visual Basic applications, use the MsgBox function)The StdIn property, StdOut property, and StdErr property can be used from Visual Basic, but they require a console program. These can also be accessed by opening $CON as a file using the proper API.
All of the methods of WSH are duplicated by other API calls that Visual Basic can access.
Although the WSH object model cannot be accessed through Visual Basic objects, the same functionality is available through the Visual Basic language and the Win32 API.