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
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
This site uses cookies to help personalise content, tailor your experience and to keep you logged in if you register.
By continuing to use this site, you are consenting to our use of cookies.