Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations bkrike on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Execute Function in VB 1

Status
Not open for further replies.

Cocheez

Programmer
Jun 6, 2001
56
US
Is there any way to get the functionality of the Execute() statement found in VBScript or eval() function in JScript into VB. It is probably right underneath my nose but I just can't seem to find an answer. Thanks !!
-Matt
 
Add a reference to the Microsoft Script Control, and you can do something like the following:

Option Explicit

Private Sub Command1_Click()
Dim wsh As ScriptControl
Dim strDemo As String

Set wsh = New ScriptControl
wsh.Language = "vbscript"
strDemo = "msgbox ""Hello"""
wsh.ExecuteStatement strDemo
End Sub
 
Your example works great, however it seems this does
not work with assignments. I am sorry I didn't explain
my intensions better before.

' This does not set intDemo to 45
Private Sub Command1_Click()
Dim wsh As ScriptControl
Dim strDemo As String
Dim intDemo As Integer

Set wsh = New ScriptControl
wsh.Language = "vbscript"
strDemo = "intDemo = 45"
wsh.ExecuteStatement strDemo
Debug.Print intDemo
End Sub

-Matt
 
You can work with the Eval statement as well to return values from the Script Control:


Private Sub Command1_Click()
Dim wsh As ScriptControl
Dim strDemo As String


Set wsh = New ScriptControl
wsh.Language = "vbscript"
strDemo = "intDemo = 45" 'intDemo is in the scope of the ScriptControl, not the VB project
wsh.ExecuteStatement strDemo
Debug.Print wsh.Eval("intDemo") 'But we can get it back like this
End Sub
 
You can create an entrie VBScript module and then execute Subs and Functions within it.
Private Sub Command1_Click()
Dim strCode As String
Dim intI As Long
' Create a Script Module
strCode = "Dim I ' Global variable" & vbCrLf & _
"Public Sub AddI(lngAdder)" & vbCrLf & _
" I = I + 25" & vbCrLf & _
"End Sub" & vbCrLf & _
"Public Function GiveI()" & vbCrLf & _
" GiveI = I" & vbCrLf & _
"End Function"
SC.Reset
SC.Language = "VBScript"
SC.AddCode strCode ' Do something to I
SC.Run "AddI", 25
intI = SC.Run("GiveI") ' Go get I
MsgBox intI Generate Forms/Controls Resizing/Tabbing Class
Compare Code (Text)
Generate Sort Class in VB or VBScript
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top