Interpret Source Statements at Runtime
& Expose Your App's Object Model
with the Script Control

Ever need to create a VB statement at run time and execute it? Wouldn't it be great if you could read a line of code or a complete procedure from a database then have your program run it? How about exposing your application's object model so users can call the methods of the objects in your application?

The Microsoft Script Control lets you create an application, called the Host application, that runs a scripting language such as VBScript or JavaScript, both of which are included with the Script Control. The scripting language statements are called Scripts.

You can add the run-time functionality of any ActiveX object to the Script Control. Run-time functionality includes any public method or property of the object. The object can be any ActiveX component, such as your forms and controls, class modules, ActiveX DLLs and so on. Adding methods of your objects to the script control exposes them and allows your users to call them dynamically while your program is running. This makes your application Scriptable.

In other words, the Script Control can behave as a macro engine to interpret source code, call the methods of a component or set a component's properties. For example, you can run your host program then type a new function into a textbox and have your application run it.

Using the Microsoft Script Control
Download Source Code

Notes
  • Download the Microsoft Script Control from the Scripting page of Microsoft's site. This is also the best reference on using ActiveX Scripting.
  • This project was written in VB6 but the concepts will work in VB5.
  • The Script Control is distributed as MSScript.ocx. To use it in your project, select Microsoft Script Control from the Project | Components dialog.
  • Scripts must abide by the syntax rules governing the scripting language, VBScript in this example.
  • Pay attention to the use of double quotes (") in the code to correctly mark string values.
  • The textboxes used must have their Multiline properties set to True.
Concepts

This sample shows how to use the Script Control to:

  • Enter individual source code statements, complete procedures or functions at run time and execute them.
  • Make the methods and properties of any control on your form available to the Scripting Engine so your script can access and manipulate them.
  • Expose procedures and functions, i.e. methods, in your class modules to the script engine so they can be called from a script. This exposes your applications object model and makes your application scriptable.
  • Retrieve error information when an error occurs in your script code.
Script Control Methods, Properties and Events

 

Property

Description

AllowUI

Sets or returns a Boolean value indicating whether a running script, or the ScriptControl itself, can display user-interface elements. For example, in VBScript if AllowUI is False, the MsgBox statement will not work.

CodeObject

Returns an object that is used to call public members of a specified module

Column

Returns the column indicating the approximate place in the scripting code where an error occurred.

Description

Returns a short descriptive error message.

HasReturnValue

Returns True if a procedure has a return value; False if it does not.

Language

Tells the Script Control to use the specified language. The default is VBScript but it can be JScript.

Line

Returns the line indicating the place in scripting code where an error occurred.

NumArgs

Returns the number of arguments required by a procedure.

Number

Returns a numeric value specifying a run-time error.

Source

Returns a string specifying the type of error that occurred.

State

Set to "Connected" immediately after an AddObject call to to turn on event handling so your script can respond to events in your application. See the code.

Text

Returns a string containing a snippet of the scripting code surrounding the location where an error occurred in code.

TimeOut

Sets or returns the time, in milliseconds, after which a user is presented with the option to discontinue scripting code execution or allow it to continue.

If value is specified using the NoTimeout constant (-1), no timeout is used. When value is 0, the Timeout event occurs as soon as it is determined that a running script is hung for any reason.

UseSafeSubset

Sets or returns a Boolean value indicating whether the host application is concerned about safety. The objects and procedures that can't be used when UseSafeSubset is True are identical to those restricted by the browser's highest security setting.

 

Method

Description

AddCode

Adds the specified code to a module.

AddObject

Makes run-time functionality available to a scripting engine. The AddObject method enables a ScriptControl user to provide a set of name/object pairs to the scripting code.

Clear

Clears all Error object properties. The properties of the Error object are also cleared when Reset, AddCode, Eval, or ExecuteStatement methods are executed.

Eval

Evaluates an expression and returns the result. The context of the Eval method is determined by the object argument. If object is a module, the context is restricted to the named module. If object is the ScriptControl, the context is global.

ExecuteStatement

Executes a specified statement. The context of the ExecuteStatement method is determined by the object argument. If object is a module, the context is restricted to the named module. If object is the ScriptControl, the context is global.

Reset

Discards all scripting code and objects that have been added to the ScriptControl.

Run

Runs a specified procedure.

 

Event

Description

Error

Occurs in response to a run-time error.

TimeOut

Occurs when the time specified in the Timeout property has elapsed, and a user has selected End from the resulting dialog box.

Instructions

To learn how to add and run your script:

  1. Open the project and set a breakpoint on the first line in procedure cmdExecute_Click.
  2. Press F5 to run the project.
  3. Copy the following code and type or paste it into the top textbox.

    Function AddTwoNumbers(X1, X2)
    Dim Result
        Result = X1 + X2
        AddTwoNumbers = Result
    End Function

    Sub Hello(sName)
        Msgbox "Hello " & sName
    End Sub

  4. Click the Execute button.
  5. Step though the code by pressing F8 and observe how the code you pasted into the textbox gets executed.
  6. Click the Quit button to end the program.
  7. Change the following line of code

    from "l = .Eval("AddTwoNumbers(5,2)")"
    to "l = .Eval("AddTwoNumbers(5,)")"

    This will produce an error since the last parameter is missing.

  8. Repeat steps 3 and 5 to see how to retrieve the error information.

To learn how to work with controls:

  1. Select a control from the listbox. For example, select lstCtrls.
  2. Click the Expose the lstCtrls Object button.
  3. Enter the name to refer to the lstCtrls button by. For example, enter lstC and click OK.
  4. Click the Run Script button. This runs a script that displays the number of controls on the form. The form’s controls collection was made available for scripting in the form load procedure.
  5. In the bottom textbox enter a VBScript statement to manipulate the control you selected. For example, enter lstC.Visible = False.
  6. Click the Run Script button and watch the listbox disappear. Now enter lstC.Visible = True and press Run Script to make it visible again.

To learn how to work with your class modules:

  1. Click the Run Class1 Procedure button to invoke the pDisplay method of class1. A parameter is passed to the method to tell it what message to display.


About TheScarms
About TheScarms


Sample code
version info

If you use this code, please mention "www.TheScarms.com"

Email this page


© Copyright 2016 TheScarms
Goto top of page