README.md

December 5, 2022 ยท View on GitHub

The Scripting Control

Description

I've seen too many submissions that say "~!!~~ WOW A++ NEW PROGRAMMING LANGUAGE MUST SEE!!". Then you open it, and its not a new language, it just uses the MS Scripting Control. And they use it WRONG too. I've written a little tutorial about this powerful control, and included two very good examples in the .zip file.

More Info

Submitted On2003-03-03 21:09:32
Byjohn sheridan
LevelBeginner
User Rating4.8 (29 globes from 6 users)
CompatibilityVB 6.0
CategoryMiscellaneous
WorldVisual Basic
Archive FileThe_Script155400332003.zip

Source Code

The Scripting Control allows users to put VBScript into your programs, to let them expand on the functions of the program.

In the zip, I included a program that teaches you how to let users use VBScript in your programs and return values based on other values. Second, I included a spreadsheet program, which 1) saves and opens its own file type 2) associates its file type so it opens when you click the file 3) lets you type in formulas into the cells like in Excel. Now for the tutorial!


First, I'll teach you how to create an instance of the Scripting Control without loading it as a component.

Sub scriptTut()
Dim scriptCTL 'dim the control
Set scriptCTL = CreateObject("ScriptControl")
'create the control

scriptCTL.language = "VBScript" 'set the language

'now you can run commands on the
ScriptControl just like a normal one
'ill teach you how to do that now

End Sub


There are many ways to use the ScriptControl. The simplest way is to use ExecuteStatement. But, it isn't as functional as other methods. Here's how to use it:

ScriptCTL.executestatement "MsgBox ""Hi!"""

'you can also set the scriptcontrol's variables quickly
ScriptCTL.executestatement "myVar = 6"


That's the simplest way. The good way is to add functions to the control and then run them. This is very good because it lets people add their own functions and then evaluate them depending on other variables. To do that, you'd do something like this:

Dim strProgram As String
strProgram = "Function popUp(str)" & vbcrlf & _
"MsgBox str" & vbCrlf & _
"End Function"

'now add the code to the control
scriptCTL.addcode strProgram
'note: you can also add the values of textBoxes, etc.

Now, to run our program's "popUp" function, simply do this:

scriptCTL.run "popUp", "penguins are cool"
'this runs "popUp", with the first
byVal as what you want to pop up!


Now here's a basic summary of this method that takes two numbers you write in textboxes and multiplies them. (In the zip, the first example does a similar thing, but lets the user input their own function!):

Private Sub Command1_Click()

'create the scriptcontrol here i'm too lazy

Dim strProgram As String
strProgram = "Function multiply(x,y)" & vbCrlf & _
"multiply=x*y" & vbcrlf & "End Sub"

ScriptCTL.addcode strProgram
MsgBox ScriptCTL.run("multiply", Text1.Text, Text2.Text)
'multiplies value of 2 textBoxes

End Sub


Another useful thing is evaluating statements. This function just returns a boolean value from the ScriptControl. Very simple. Here's an example:

ScriptCTL.executestatement "x = 1"
MsgBox ScriptCTL.eval("x=1") 'returns true
MsgBox ScriptCTL.eval("x-5=x*x+2") 'false



Error Messages: This can let you (the deugger), or your user know when an error has occured. This is how to do it:

Private Sub Command1_Click()
'create scriptcontrol here

ScriptCTL.executestatement "x=3/0"
'(dividing by zero is not allowed in math)

On Error Goto errHan
Exit Sub

errHan:
Debug.Print ScriptControl1.Error.Number & _
":" & ScriptControl1.Error.Description & _
" in line " & ScriptControl1.Error.Line

End Sub



Well, that concludes this basic tutorial of the Scripting Control. There is A LOT more you can do (like add code from modules in your VB project). This can all be found in the help file that comes with windows (at least in XP). It can be found in Windows\System32 and is named "MSScript.hlp". I hope you enjoyed and learned from this tutorial. Please download the sample code, it is very good. Please vote and give feedback, I spent a really long time on the programs and this tutorial. :-)