需要首先引用 Microsoft Script Control:在工程中创建一个class module,然后在其中加入代码: Public k1 As Integer在Form中添加一个TextBox,一个commandbutton和一个ScriptControl:Private Sub Command1_Click() Dim a(1) Dim shar As New Class1
ScriptControl1.AddObject "sh", shar ScriptControl1.AddCode "Function Main(x)" & vbCrLf & "kk=" & Text1.Text & vbCrLf & "sh.k1 = kk " & vbCrLf & "End Function" ScriptControl1.Run "main", a MsgBox "结果等于:" & shar.k1, , "运算结果" End Sub Private Sub Form_Load() Text1.Text = "3+2*1" End Sub
Private Declare Function EbExecuteLine Lib "vba6.dll" _ (ByVal StringToExec As Long, ByVal Any1 As Long, _ ByVal Any2 As Long, ByVal CheckOnly As Long) As LongDim Variable As VariantFunction ExecuteCode(Code As String, Optional CheckOnly As Boolean) As Boolean ExecuteCode = EbExecuteLine(StrPtr(Code), 0&, 0&, CheckOnly) = 0 End Function Private Sub cmdCheckCode_Click() Dim Code As String Code = Replace(txtCode.Text, vbCrLf, ":") MsgBox ExecuteCode(Code, True) End SubPrivate Sub cmdExecuteCode_Click() Dim Code As String Code = Replace(txtCode.Text, vbCrLf, ":") Call ExecuteCode(Code) End SubPrivate Sub Command1_Click() MsgBox a End SubPrivate Sub Form_Load() txtCode.Text = "a=1+2*3" + vbCrLf + "Variable=a" + vbCrLf + "msgbox a" Label1.Caption = "请在文本框中输一段代码,看看效果" cmdExecuteCode.Caption = "执行代码" cmdCheckCode.Caption = "执行结果" End Sub
//vba6可不小啊,有没有系统里的API Private Declare Function EbExecuteLine Lib "vba6.dll" _ (ByVal StringToExec As Long, ByVal Any1 As Long, _ ByVal Any2 As Long, ByVal CheckOnly As Long) As Long用的就是api函数呀,一般用户的机器上都应该有vba6.dll
Public k1 As Integer在Form中添加一个TextBox,一个commandbutton和一个ScriptControl:Private Sub Command1_Click()
Dim a(1)
Dim shar As New Class1
ScriptControl1.AddObject "sh", shar
ScriptControl1.AddCode "Function Main(x)" & vbCrLf & "kk=" & Text1.Text & vbCrLf & "sh.k1 = kk " & vbCrLf & "End Function" ScriptControl1.Run "main", a
MsgBox "结果等于:" & shar.k1, , "运算结果"
End Sub
Private Sub Form_Load()
Text1.Text = "3+2*1"
End Sub
(ByVal StringToExec As Long, ByVal Any1 As Long, _
ByVal Any2 As Long, ByVal CheckOnly As Long) As LongDim Variable As VariantFunction ExecuteCode(Code As String, Optional CheckOnly As Boolean) As Boolean
ExecuteCode = EbExecuteLine(StrPtr(Code), 0&, 0&, CheckOnly) = 0
End Function
Private Sub cmdCheckCode_Click()
Dim Code As String
Code = Replace(txtCode.Text, vbCrLf, ":")
MsgBox ExecuteCode(Code, True)
End SubPrivate Sub cmdExecuteCode_Click()
Dim Code As String
Code = Replace(txtCode.Text, vbCrLf, ":")
Call ExecuteCode(Code)
End SubPrivate Sub Command1_Click()
MsgBox a
End SubPrivate Sub Form_Load()
txtCode.Text = "a=1+2*3" + vbCrLf + "Variable=a" + vbCrLf + "msgbox a"
Label1.Caption = "请在文本框中输一段代码,看看效果"
cmdExecuteCode.Caption = "执行代码"
cmdCheckCode.Caption = "执行结果"
End Sub
http://www.freevbcode.com/code/Parser.zip
Private Declare Function EbExecuteLine Lib "vba6.dll" _
(ByVal StringToExec As Long, ByVal Any1 As Long, _
ByVal Any2 As Long, ByVal CheckOnly As Long) As Long用的就是api函数呀,一般用户的机器上都应该有vba6.dll
Registers:
EAX=00000000 CS=0167 EIP=0fa916aa EFLGS=00000206
EBX=00000096 SS=016f ESP=0067eccc EBP=0067ece0
ECX=0067ed84 DS=016f ESI=0067ed84 FS=2eaf
EDX=81730000 ES=016f EDI=00000096 GS=0000
Bytes at CS:EIP:
8b 08 50 ff 51 0c c2 04 00 6a 01 58 84 05 e4 c0
Stack dump:
00000096 0fa91a89 00000096 00000000 0000004b 0067ed94 0fbb6ad9 00000078 00000000 00000096 0067ed0c 00000000 00000001 0067edf4 0067f290 00000001