Examples of usages of Msscriptcontrol.scriptcontrol components _vbs

Source: Internet
Author: User
Tags eval object model

The Microsoft (r) Script control enables users to create running any ActiveX (r) scripting engine, such as Microsoft (r) Visual Basic (r) scripting Edition or Microsoft (r) A JScript (TM) application. The user can add the object model of any automation object to the Script control so that the object's methods and properties can be used by the scripting engine. By synthesizing an application's object model and a scripting engine, users can create a scripting application that combines two benefits. The application not only has the simplicity of scripting language, but also integrates a more advanced, fully-featured object, method, and attribute of a professional application.

To create an instance of a Script control

The Microsoft Script control can be created as a control or as a stand-alone 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. The following code works as long as the Script control appears and registers:

Copy Code code 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
Sc.run "Main"
End Sub

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

Instance:

Copy Code code as follows:

Dim SC as ScriptControl

Displaying the user interface element Allowui property determines whether the scripting engine can display user interface elements. This can be applied to the Script control itself, such as displaying timeout messages.

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

Copy Code code 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" is not allowed ui!

Create scripting code

Microsoft Script controls enable users to create an application that runs scripting languages, such as VBScript or JScript. For example, suppose a user has a button on a form that the user wants to run some VBScript code when the button is pressed. This button is called Run Nameme, and the user wants scripting code to run a process called Nameme. The intention is that the VBScript scripting engine executes the nameme process when the user clicks the Run nameme button.

The Run Nameme button uses the script control's Run method to execute scripts. The following is the code that should appear in the Click event of the Run nameme button:

Copy Code code as follows:

' The name of the Script control is ScriptControl1.
Private Sub Runnameme_click ()
Scriptcontrol1.run "Nameme"
End Sub

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

Choose a scripting language

The first step is to configure the correct scripting language for Script control. The Language property is automatically initialized to "VBScript" when the Script control is created on a page as a controls. When the Script control is created as a Automation object, the Language property is left as a preliminary
The state of the initialization, which must be set by the author of the Code.

To set the Language property to JScript, you can use the Properties window. Users can also use the Language property in their code, such as the
Shown

Copy Code code as follows:

Scriptcontrol1.language = "JScript"

Other scripting languages, such as PERL and REXX, are not provided by Microsoft or can be used for Script controls.

Adding 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 a user attempts to add an incomplete procedure (a procedure that has no end Sub or end Function), an error occurs. The following code adds the procedure code to the Script
Control:

Copy Code code as follows:

' When the Scriptrun application loads, add the following code
' Add the Nameme procedure to the 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

Alternatively, users can add process code from a TextBox control:

Copy Code code as follows:

Private Sub Form_Load ()
' This code is contained on a form named Frmscript
' In a textbox called Txtscript.
Scriptcontrol1.addcode FrmScript.txtScript.Text
End Sub

The user can add parameters to a procedure or function.

Copy Code code as follows:

Private Sub Evalfunc ()
' Create a function.
Dim Strfunction as String
Strfunction = _
"Function returnthis (x, y)" & VbCrLf & _
"Returnthis = x * y" & vbCrLf & _
"End Function"
' Add code, and then run the function.
Scriptcontrol1.addcode strfunction
MsgBox Scriptcontrol1.run ("Returnthis", 3, 25)
End Sub

Run 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 Code code as follows:

Scriptcontrol1.run "FindName"
Scriptcontrol1.run "AddName"
Scriptcontrol1.run "Quit"

Executes the scripting statement and calculates the result, and the user can execute a scripting statement using the Executestatement method. The user can use the Eval method to evaluate an expression value. In the following example
, the value 100 is assigned to the variable x by the Executestatement method. The following two lines use the Eval method to test statements x = 100 and x = 100/2. The second line returns TRUE, and the third line returns FALSE.

Copy Code code 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. In order 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
Description, and the line number where the error appears in the script.

Run the following code to see an example of a Script control finding an error:

Copy Code code as follows:

Private Sub Myerror ()
' The following code is 0 in addition to causing
' A mistake.
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
Scerror:
' Use the Error object to advertise to the user
' Error, and line of error.
Debug.Print ScriptControl1.Error.Number & _
":" & ScriptControl1.Error.Description & _
"In line" & ScriptControl1.Error.Line
Exit Sub
End Sub

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.