# 字串变数的????我大概解出来了喔!!

Posted by 小瓜呆 on October 14, 1998 at 15:34:04:

In Reply to: 看了所有的讨论, 还是莫宰羊---字串变数的???? posted by 笨鸟而且慢飞 on October 07, 1998 at 01:02:40:

里面有一些是微软站的资料!!供大家参考!!
Command4_click() 才是本题正解!!

Private Sub Command1_Click()
Dim sc
Dim strProgram As String
strProgram = "Sub Main" & vbCrLf & _
"MsgBox ""Hello World""" & vbCrLf & _
"End Sub"

Set sc = CreateObject("ScriptControl")
sc.Language = "VBScript"
sc.AddCode strProgram
sc.Run "Main"
End Sub

Private Sub Command2_Click()
ScriptControl1.AllowUI = True
Dim strX As String
strX = "Sub Hello" & vbCrLf & _
"MsgBox ""Hello World""" & vbCrLf & _
"End Sub"
ScriptControl1.AddCode strX
ScriptControl1.Run "Hello" ' No UI allowed!
End Sub

Private Sub Command3_Click()
MsgBox ("EvalFunc")
EvalFunc
MsgBox ("Try This")
TryThis
'MsgBox ("MyErr")
'MyError
End Sub

Private Sub Command4_Click()
Para1 = """String1""" '因为在 script control 内必须为字串
Para2 = """String2"""
With ScriptControl1
.Language = "VBScript"
.ExecuteStatement "Xstr1=" + Para1 ' 必须是 "Xstr1 = ""MyStr"" " 的型态
.ExecuteStatement "Xstr2=" + Para2
'.ExecuteStatement "test"
'.ExecuteStatement "MsgBox AB"
End With
qq = ScriptControl1.Eval("Xstr1+Xstr2")
MsgBox qq
End Sub
Private Sub command5_click()
Dim strCode As String
strX = "Sub SayHello" & vbCrLf & _
"MsgBox ""Hello World""" & vbCrLf & _
"End Sub"
strCode = " Sub NameMe() " & vbCrLf & _
" Dim strName As String " & vbCrLf & _
" strName = InputBox(""Name?"")" & vbCrLf & _
" MsgBox ""Your name is "" & strName " & vbCrLf & _
"End Sub"
ScriptControl1.Language = "VBScript"
MsgBox strCode
ScriptControl1.AddCode (strX)
ScriptControl1.Run "SayHello"
ScriptControl1.AddCode (strCode) '不知问题在那里!!
ScriptControl1.Run "NameMe"
End Sub
Private Sub command6_click()
' The code is contained in the Textbox named
' txtScript on the form named frmScript.
'ScriptControl1.AddCode myfrm.mytxt.Text
End Sub

'You can add arguments to a procedure or function.

Private Sub EvalFunc()
' Create the function.
Dim strFunction As String
strFunction = "Function ReturnThis(x, y)" & vbCrLf & _
" ReturnThis = x * y" & vbCrLf & _
"End Function"
' Add the code, then run the function.
ScriptControl1.AddCode strFunction
MsgBox ScriptControl1.Run("ReturnThis", 3, 25)
End Sub

'-----Run the Procedure
'The Run method runs any complete procedure that has been added to the Script Control. The following code fragment runs three defined procedures:

'ScriptControl1.Run "FindName"
'ScriptControl1.Run "AddName"
'ScriptControl1.Run "Quit"
'-----Executing Scripting Statements and Evaluating the Results

'You can execute a scripting statement using the ExecuteStatement method. You can evaluate an expression using the eval method. In the following example, the ExecuteStatement method is used to assign the value 100 to variable x. The next two lines use the eval method to test the statements x = 100 and x = 100/2. The second line returns True; the third line returns False.
Private Sub TryThis()
ScriptControl1.ExecuteStatement "x = 100"
MsgBox ScriptControl1.Eval("x = 100") ' True
MsgBox ScriptControl1.Eval("x = 100/2") ' False
End Sub

'-----sing the Error Property

'Script Control errors may come from two sources: the Script Control itself, or the script that the Control is attempting to run. To debug scripting code, use the Error property, which returns a reference to the Error object. Using the Error object, the Script Control can return the number and description of the error and also the line number where the error occurs in the script.
'Run the following code to see an example of the Script Control finding an error:

Private Sub MyError()
' The scripting code below divides by zero raising
' an error.
Dim strCode As String
strCode = "Sub DivideByZero()" & vbCrLf & _
"Dim prime" & vbCrLf & _
"prime = 3" & vbCrLf & _
"MsgBox prime/0" & vbCrLf & _
"End Sub"
On Error GoTo scError
With ScriptControl1
.AddCode strCode
.Run "DivideByZero"
End With
Exit Sub
scError:
' Use the Error object to inform the user of the
' error, and what line it occurred in.
Debug.Print ScriptControl1.Error.Number & _
":" & ScriptControl1.Error.Description & _
" in line " & ScriptControl1.Error.Line
Exit Sub
End Sub

Follow Ups: