Search Tools Links Login

A quick course of making scriptable program, Like the VBA (Very Cool!)


Scriptable make
everything possible possible


Have you ever use the VBA in Microsoft Office? Making your application
scriptable can enable it's functions to be extent to infinite, by the End Users.
End Users can "WRITE PROGRAM ON YOUR PROGRAM", and run it as they
like. It sounds interesting?

Original Author: Kenny Lai, Lai Ho Wa

Code

A quick course of making scriptable program,
Like the VBA (Very Cool!)


Scriptable make
everything possible possible


Have you ever use the VBA in Microsoft Office? Making your application
scriptable can enable it's functions to be extent to infinite, by the End Users.
End Users can "WRITE PROGRAM ON YOUR PROGRAM", and run it as they
like. It sounds interesting?


@


This is a quick course teaching you how to make you application scriptable,
using Microsoft Scripting Control.




Understanding Microsoft
Scripting Control


This is a free gift come together with Visual Basic. It support VBScript and
JScript. But for convinence, I will use VBScript for demonstration. 


It is very easy to use. Let's say we have a script control SC


Private Sub Command1_Click()



    Dim strProgram
As String



strProgram = "Sub Main" & vbCrLf & _

"MsgBox ""Hello World""" & vbCrLf & _

"End Sub"



sc.language = "VBScript"




sc.addcode strProgram

sc.run "Main"





End Sub


A message box will appear when you press Command1. The code is in VBScript
format(*) and can be enter by any method you like, said TextBox. This enable
end-users entering their own VBScript code they like, and run them. It just like
another Visual Basic!


(* The main difference is that the only varible type is viarant. e.g. Dim
a,b,c but NOT Dim a as string)


So, what can you do to make your application scriptable, extentable?




Program Overview


Now, right click on the controls list and add a reference to "Microsoft
Script Control 1.0". Create one on a form.




  
  


  
  


  
  


  
  


  
  


  
  


  
  


  
  

NameType
SCMicrosoft Script Control
Form1Form
Text1TextBox1
txtCodeTextBox
txtCommandTextBox
lstProceduresListBox
CmdRunCommand Button

@


The Text1 is used as an object that is the "Scriptable" part. In
this program, end users can enter Visual Bascis SCRIPT code in txtCode. They may
run the code by entering command lines in txtCommand, and press CmdRun.




The main part


There is a AddObject function in the script control. You can add any object,
controls, like textbox, forms, buttons and picture box into the script control,
and give them a "scripting name", i.e. the name used to identify the
object in the end-users code.


Private Form_Load


sc.AddObject "MyText", Text1


End Sub


After add the Text1 into the script control, you can access the Text1 in the
End-users code that is entered in the txtCode.


e.g.


In the txtCode, enter the following code:


Sub Main


    Msgbox MyText.Text


End Sub


Also add the following code to the program(Not the textbox)


Private Sub CmdRun_Click


    sc.run "Main"


End Sub


Private Sub sc_Error()


    MsgBox "Error running code: " & SC.Error.Description & vbCrLf & "Line:" &
SC.Error.Line


End Sub


When you click the CmdRun, the code in the txtCode Sub Main section will be
run. Now you can see the how easy to control the program by end-uses code. The
"Msgbox ..." can be replace by any logical VBScript code. E.g. if you
entered MyText.visible=False, the textbox will disappear.


Similarly, you can AddObject of any controls and object you like into script
control and control it totally by end-users code. This is the basis of making
scriptable application.


Futhermore, the script control provide the procedures object so that you can
get all information of the procedures of your code.


Private Sub txtCode_Change


    On Error Resume Next


    lstProcedures.Clear


    Dim i as
integer


    For i=1 to
sc.Procedures.Count


        lstProcedures.Additem
sc.Procedures(i)


    Next i


End Sub


Sub ExecuteCommand(Str As
string)


    On Error Goto 1


    sc.ExecuteStatement Str


Exit Sub


1


Msgbox Error


End Sub


For the ExecuteCommand, your can enter a correct statement to execute like:


Msgbox MyText.Text


Main


MyProcedures Arg1,Arg2


Msgbox MyFunction(Arg1, Arg2, Arg3)




Demonstration Program


And by now, you may be able to create your scriptable program, or make a
scripting console for your application.


Here is my demonstration program, my Page Creator 3. I don't like to write
just a simple program.


On the Left hand Outlook bar, click on the PCScript Console to open the
console panel. Open a template by clicking open. After entering your own code
you like, return the main program or the HTML Code Editor. Find the tab
"PCScript" on the floating toolbox. There is a command window. Just
type the command in the textbox and click return key to execute it. Have Fun!


Kenny Lai


Download Demonstration:


http://student.mst.edu.hk/~s9710050/Page
Creator 3.zip


@

About this post

Posted: 2002-06-01
By: ArchiveBot
Viewed: 127 times

Categories

Visual Basic 6

Attachments

No attachments for this post


Loading Comments ...

Comments

No comments have been added for this post.

You must be logged in to make a comment.