Microsoft Script Control

bobcory
10-19-2010, 04:17 AM
I have been experimenting with the "Microsoft Script Control" because I need users to be able to write and run scripts from within my application. If you have not used it, here is an example

First you need to get it by:
'Add the "Microsoft Script Control" by Project --> Components --> Microsoft Script Control 1.0
'Also use Project --> References --> Microsoft Script Control 1.0

Or, failing that you can download the .ocx from Microsoft at:
http://www.microsoft.com/downloads/en/details.aspx?FamilyId=D7E31492-2595-49E6-8C02-1426FEC693AC&displaylang=en


Dim strA As String

scrScript.Language = "VBScript"

scrScript.AddObject "MyForm", Me

'Disable a Button
strA = "MyForm.cmdContinue.Enabled = False"
scrScript.ExecuteStatement strA

'Change the Caption
strA = "MyForm.Caption = " & Chr(34) & "It worked!" & Chr(34)
scrScript.ExecuteStatement strA

'Change a Public Variable
strA = "MyForm.strDemo = " & Chr(34) & "This worked too!" & Chr(34) & ": MsgBox MyForm.strDemo"
scrScript.ExecuteStatement strA

'Add some Code (note that lines are separated by colons)
scrScript.AddCode "Function Calc(X): Calc = X * 3: MsgBox Calc: End Function"
scrScript.AddCode "Function Square(X): Square = X ^ 2: MsgBox Square: End Function"

'Run the Code
scrScript.Run "Calc", 7
scrScript.Run "Square", 7


There is a good article here:
http://msdn.microsoft.com/en-us/library/ms974586.aspx

Now to the question: Does anybody know if, or how, this can be extended so that users can add Methods and/or Properties to one of my Classes?
Thanks
Bob
.

Gruff
10-19-2010, 10:02 AM
It's been a while since I used the Script Control but I'll take a wack at it.

In short if your class is compiled then no. No more than you could add a property to a compiled class in standard VB6. If however you kept your class source code in a text file or someother form of storage you could read the file, programmically insert their properties and methods into the text string at the proper points, then push the code into your script control.

The bad part about the script control is that it provides none of the really great feed back that the VB6 IDE does. Your users are left typing in code blindly. Running the code and having it crash.

One way around this is to have them use Excel VBA and its IDE as a developing platform for writing and developing code for the script control. If they follow the scripting code rules (Turn off Option Excplicit and make all non objects variant data types) VBA will still debug and interpret the code.

In my case I made a dll version of my end application complete with embedded forms. They referenced the dll in VBA and wrote their routines. Then cut and pasted them into the Actual app that contained the Script control.

The downside to all of this is that since none of the users had experience programming they had an immense problem writing proper code regardless of where they did it.

Gruff
10-20-2010, 07:49 AM
Rough Sample of defining and using a VBScript Class


Option Explicit

Private Sub Command1_Click()
' SC1 is your MS Script Control
SC1.Reset
SC1.Language = "VBScript"
SC1.AllowUI = True

Dim s As String

s = ""
s = s & "Class Greeter" & vbCrLf

s = s & " Dim m_UserName" & vbCrLf
s = s & "" & vbCrLf

s = s & " Public Property Get UserName()" & vbCrLf
s = s & " UserName = m_UserName" & vbCrLf
s = s & " End Property" & vbCrLf
s = s & "" & vbCrLf

s = s & " Public Property Let UserName(sValue)" & vbCrLf
s = s & " m_UserName = sValue" & vbCrLf
s = s & " ' Validation statements go here." & vbCrLf
s = s & " End Property" & vbCrLf

s = s & "" & vbCrLf
s = s & "" & vbCrLf

s = s & " Sub Hi()" & vbCrLf
s = s & " Msgbox " & Chr(34) & "Hello " & Chr(34) & " & m_UserName" & vbCrLf
s = s & " End Sub" & vbCrLf
s = s & "End Class" & vbCrLf

s = s & "" & vbCrLf
s = s & "Sub Greetings" & vbCrLf
s = s & " Dim oGreet" & vbCrLf
s = s & " Set oGreet = New Greeter" & vbCrLf

s = s & " oGreet.UserName = " & Chr(34) & "Mike" & Chr(34) & vbCrLf
s = s & " oGreet.Hi" & vbCrLf
s = s & " Set oGreet = Nothing" & vbCrLf
s = s & "End Sub" & vbCrLf

SC1.AddCode s
SC1.ExecuteStatement "Greetings"

End Sub

EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum