0

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.

1
  • A similar error can be generated if you run the same script in this app: scalabium.com/smscript.htm Commented Feb 3, 2016 at 18:20

2 Answers 2

2

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.

Sign up to request clarification or add additional context in comments.

Comments

0

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_")

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.