Microsoft KB Archive/183284

= INFO: CreateObject in VBScript =

Article ID: 183284

Article Last Modified on 8/27/2007

-

APPLIES TO


 * Microsoft Visual Basic, Scripting Edition 2.0
 * Microsoft Visual Basic, Scripting Edition 3.0

-



This article was previously published under Q183284



SUMMARY
The versions (1.0) of the VBScript engine that shipped with Internet Explorer 3.0x did not support the CreateObject function, which is commonly available in Visual Basic for Applications and Visual Basic.

As of version 2.0, VBScript supports the CreateObject function. If the 2.0 (or higher) version of these engines has been installed on a client machine, the CreateObject function is supported in client-side script embedded inside an HTML page when using Internet Explorer 3.0x.



MORE INFORMATION
VBScript 2.0 shipped with the Active Server Pages (ASP) component of Internet Information Server (IIS) 3.0. ASP is used to produce server-side script inside .asp files. ASP has always supported CreateObject. The rest of this article refers to the use of CreateObject in client-side script, which is evaluated by Internet Explorer 3.x, not ASP server-side script.

The VBScript CreateObject function works in the conventional manner: it takes one string parameter and returns an object. The string parameter should refer to the ProgID of a valid Automation object installed on the client system. Here is an example CreateObject call: Set MyObject = CreateObject("MyObject.TestObject") Keep in mind that there is no guarantee that all client machines viewing a Web page will have a particular object installed. When using the  tag for controls, a CODEBASE URL can be specified to provide for automatic download of the control. With CreateObject, there is no such mechanism.

When using CreateObject, you may get the following scripting error: [Line: xx] Object not safe for scripting: 'Application.Object' This error message indicates that the object cannot be safely used within a scripting environment. Either the object can be used to do some potentially unsafe act such as write to the hard disk or set registry entries, or the object just is not guaranteed to work correctly from within a protected script language. Objects that are unsafe for scripting should not be scripted in VBScript.

For information on marking objects safe for scripting, please see the following articles in the Microsoft Knowledge Base:

164119 SAMPLE: Implementing IObjectSafety in an ActiveX Control

161873 HOWTO: Mark MFC Controls Safe for Scripting/Initialization

Note that the registry entries mentioned in these articles could be used on a client machine to mark any object as safe for scripting. This is recommended only in internal environments where the safety of objects in Web pages can be guaranteed and the use of an object that is not safe for scripting is necessary. These registry entries need to be applied by the client and cannot be forced on a client through script code.

To conditionally test for the engine version currently installed on the client machine, and consequently whether the client supports CreateObject, the ScriptEngineMajorVersion function can be used in both VBScript and JScript. Here is an example snippet of VBScript code: If ScriptEngineMajorVersion >= 2 Then ' CreateObject is supported - use it  Else MsgBox "Please download the latest version of VBScript" End If NOTE: In JScript 3.0, the new ActiveXObject function was introduced to perform the same function as CreateObject in VBScript.

