Walid
Walid

Reputation: 197

VBscript error: Object required: 'wscript'

Could someone tell me why does this line error:

set my_obj = wscript.CreateObject("ObjectTest","pref_")

It gives this error:

Object required: 'wscript'

If I run this code:

Set WScript = CreateObject("WScript.Shell")
set my_obj = CreateObject("ObjectTest","pref_")

I get this error instead:

Object doesn't support this property or method: 'CreateObject'

I'm running the vbscript from within a Delphi app.

Upvotes: 0

Views: 11034

Answers (2)

OhStylerrYT
OhStylerrYT

Reputation: 3

The reason your script is failing is due to a few occurrences.

  1. DO NOT use "WScript" as an object especially while coding in Windows-Based Script Host(or WSH), as the program assumes you are running wscript.exe or calling WScript commands.
  2. Use Dim! If the first rule wasn't null, this is why. Using command "Option Explicit" in VBScript requires users to Dim any object or calling of an object.

A fixed code would be:

Option Explicit
Dim 1, 2
Set 1 = WScript.CreateObject("WScript.Shell")
Set 2 = WScript.CreateObject("ObjectTest", "pref_")

Upvotes: 0

Remy Lebeau
Remy Lebeau

Reputation: 596236

Object required: 'wscript'

I'm running the vbscript from within a Delphi app.

This is why your script is failing. The wscript object is only defined when the script is run by wscript.exe. To do what you are attempting, you need to implement your own object and provide it to the script environment for the script code to access when needed.

Assuming you are using IActiveScript to run your script, you can write a COM Automation object that implements the IDispatch interface, and then you can create an instance of that object and give it to the IActiveScript.AddNamedItem() method before then calling IActiveScript.SetScriptState() to start running the script.

For example, write an Automation object that exposes its own CreateObject() method, give it to AddNamedItem() with a name of App, and then the script can call App.CreateObject(). Your CreateObject() implementation can then create the real requested object and hook up event handlers to it as needed. To fire events back into the script, use the IActiveScript.GetScriptDispatch() method to retrieve an IDispatch for the desired procedure defined in the script, and then use IDispatch.Invoke() with DISPID 0 and the DISPATCH_METHOD flag to execute that procedure with the desired input parameters.

Your object can implement any properties and methods that you want the script to have access to.

Upvotes: 2

Related Questions