Microsoft KB Archive/279164

= Certain WScript methods and properties not accessible in Visual Basic =

Article ID: 279164

Article Last Modified on 5/25/2005

-

APPLIES TO


 * Microsoft Windows Scripting Host 2.5
 * Microsoft Windows Scripting Host 2.5
 * Microsoft Visual Basic 4.0 Professional Edition
 * Microsoft Visual Basic 5.0 Professional Edition
 * Microsoft Visual Basic 6.0 Professional Edition
 * Microsoft Visual Basic 4.0 Enterprise Edition
 * Microsoft Visual Basic 5.0 Enterprise Edition
 * Microsoft Visual Basic 6.0 Enterprise Edition

-



This article was previously published under Q279164



SUMMARY
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.



MORE INFORMATION
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(&quot;WScript.Network&quot;) WScript.Echo objWSH.Userdomain WScript.Echo objWSH.Username

ASP Sample <% Set objWSH = Server.CreateObject(&quot;WScript.Network&quot;) Response.Write objWSH.Userdomain Response.Write objWSH.Username %>

Visual Basic Sample Set objWSH = CreateObject(&quot;WScript.Network&quot;) MsgBox objWSH.Userdomain MsgBox 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 1000 objWSH = WScript.Version objWSH = 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:
 * Properties
 * Application
 * Arguments
 * FullName
 * Name
 * Path
 * ScriptFullName
 * ScriptName
 * Version
 * Methods
 * Echo (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 &quot;References&quot;]).
 * 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.

