MSScriptControl. Usage examples of scriptcontrol components


The Microsoft (R) script control enables you to create applications running any ActiveX (R) scripting engine, such as Microsoft (R) visual basic (R) Scripting Edition or Microsoft (R) JScript (TM). The user can add the object model of any automation object to the script control, so that the methods and properties of the object can be used by the scripting engine. By integrating the object model of an application with a scripting engine, users can create a scripting application that combines the two advantages. The application not only has the simplicity of scripting language, but also integrates the objects, methods and attributes of a higher-level professional application with complete characteristics.

Create an instance of a script control

The Microsoft script control can be created as a control or as a separate automation object. This feature allows applications written in any language to host any compatible scripting language with scriptcontrol.

The following example can use any format. Note that the variable SC is not declared as type scriptcontrol because the control is not and does not need to be referenced in the project. As long as the script control appears and is registered, the following code will work properly:


Copy codeThe code is as follows:

Private Sub Command1_Click()
Dim sc
Dim strProgram As String
strProgram = “Sub Main” & vbCrLf & _
“MsgBox “”Hello World””” & vbCrLf & _
“End Sub”
Set sc = CreateObject(“ScriptControl“)
sc.language = “VBScript”
sc.addcode strProgram “Main”
End Sub


If the user explicitly references the script control in the reference in his own project, the user can create a script control with the following code


Copy codeThe code is as follows:

Dim sc As ScriptControl


Displaying user interface elements the allowui property determines whether the scripting engine can display user interface elements. This can be applied to the script control itself, such as displaying a timeout message.

This can also be applied to the scripting engine that uses the ActiveX scripting interface. For example, the following code will generate an error when trying to display the Hello world message box:

Copy codeThe code is as follows:

ScriptControl1.AllowUI = False
Dim strX As String
strX = “Sub Hello” & vbCrLf & _
“MsgBox “”Hello World””” & vbCrLf & _
“End Sub”
ScriptControl1.AddCode strX
ScriptControl1. Run “hello” has no allowed UI!


Creating scripting code

The Microsoft script control enables you to create an application that runs a scripting language, such as VBScript or JScript. For example, suppose a user has a button on a form, and the user wants to run some VBScript code when the button is pressed. This button is called run nameme, and the user wants the scripting code to run a process called nameme. The intention is that when the user clicks the run nameme button, the VBScript scripting engine executes the nameme process.

The run nameme button uses the run method of script control to execute the script. Here is the code that should appear in the click event of the run nameme button:

Copy codeThe code is as follows:

‘the name of script control is scriptcontrol1.
Private Sub RunNameMe_Click()
ScriptControl1.Run “NameMe”
End Sub


To create the rest of the code needed for the script, select a scripting language, add the code to a procedure, and then run the procedure.

Select a scripting language

The first step is to configure the correct scripting language for script control. When a script control is created as a control on a page, the language property is automatically initialized to “VBScript”. When a script control is created as an automation object, the language property is left as unused
The state of initialization, which must be set by the code author.

To set the language property to JScript, use the properties window. You can also use the language attribute in your code, as shown below

Copy codeThe code is as follows:

ScriptControl1.Language = “JScript”

Other scripting languages, such as Perl and rexx, are not provided by Microsoft and can also be used by script controls.


Add code to a procedure

Before the user runs the nameme procedure, you can use the addcode method to add the complete procedure to the script control. If the user tries to add an incomplete procedure (a procedure without end sub or end function), an error will occur. The following code adds procedure code to the script
In control:

Copy codeThe code is as follows:

‘when the scriptrun application loads, add the following code
‘add nameme procedure to control.
Private Sub Form_Load()
Dim strCode As String
strCode = “Sub NameMe()” & vbCrLf & _
” Dim strName As String” & vbCrLf & _
” strName = InputBox(“”Name?””)” & vbCrLf & _
” MsgBox “”Your name is “” & strName” & vbcrLf & _
“End Sub”
ScriptControl1.AddCode strCode
End Sub


In addition, you can add process code from a TextBox control:

Copy codeThe code is as follows:

Private Sub Form_Load()
‘This code is contained on the form named frmscript
‘in a textbox named txtscript.
ScriptControl1.AddCode frmScript.txtScript.Text
End Sub


You can add parameters to a procedure or function.

Copy codeThe code is as follows:

Private Sub EvalFunc()
‘create function.
Dim strFunction As String
strFunction = _
“Function ReturnThis(x, y)” & vbCrLf & _
” ReturnThis = x * y” & vbCrLf & _
“End Function”
‘add code and run the function.
ScriptControl1.AddCode strFunction
MsgBox ScriptControl1.Run(“ReturnThis”, 3, 25)
End Sub


Operation process

The run method runs any complete procedure that has been added to the script control. The following code snippet runs three defined procedures:

Copy codeThe code is as follows:

ScriptControl1.Run “FindName”
ScriptControl1.Run “AddName”
ScriptControl1.Run “Quit”


Execute the scripting statement and calculate the result. Users can execute a scripting statement with the executestatement method. You can use the eval method to evaluate an expression. In the following example
In, use the executestatement method to assign the value 100 to the variable x. The following two lines test statements x = 100 and x = 100 / 2 with the eval method. The second line returns true; The third line returns false.


Copy codeThe code is as follows:
Private Sub TryThis()
ScriptControl1.ExecuteStatement “x = 100”
MsgBox ScriptControl1. Eval (“x = 100”) ‘returns true
MsgBox ScriptControl1. Eval (“x = 100 / 2”) ‘returns false
End Sub


Using the error property

There are two possible sources of script control errors: the script control itself, or the script that the control is trying to run. To debug scripting code, you can use the error property, which returns a reference to the error object. With the error object, the script control can return the number of errors and the response to it
Description, and the line number of the error in the script.

Run the following code to see an example of an error found by the script control:

Copy codeThe code is as follows:

Private Sub MyError()
‘the following code is divided by zero
‘an error.
Dim strCode As String
strCode = _
“Sub DivideByZero()” & vbCrLf & _
“Dim prime” & vbCrLf & _
“prime = 3” & vbCrLf & _
“MsgBox prime/0” & vbCrLf & _
“End Sub”
On Error GoTo scError
With ScriptControl1
.AddCode strCode
.Run “DivideByZero”
End With
Exit Sub
‘use the error object to notify the user
‘error, and the error line.
Debug.Print ScriptControl1.Error.Number & _
“:” & ScriptControl1.Error.Description & _
” in line ” & ScriptControl1.Error.Line
Exit Sub
End Sub