在vb程序中执行vb代码(在vb程序将字符串转为vb代码执行)

'添加一个Command,一个Text

Private Declare Function EbExecuteLine Lib "vba6.dll" (ByVal pStringToExec As Long, ByVal Unknownn1 As Long, ByVal Unknownn2 As Long, ByVal fCheckOnly As Long) As Long

Function ExecuteLine(sCode As String, Optional fCheckOnly As Boolean) As Boolean
ExecuteLine = EbExecuteLine(StrPtr(sCode), 0&, 0&, Abs(fCheckOnly)) = 0
End Function

Private Sub Command1_Click()
Dim dm() As String
Dim i As Long
dm = Split(Text1, vbCrLf)
For i = 0 To UBound(dm)
If Len(dm(i)) <> 0 Then
Dim Result As Boolean
Result = ExecuteLine(dm(i))
If Result = False Then Debug.Print i + 1 & vbTab & "行执行错误"
Else
Debug.Print i + 1 & vbTab & "行代码错误"
End If
Next
End Sub

 

以上程序执行后可在Text1中输入VB代码(如:Msgbox "VB"),点击Command1即可执行Text1中的VB代码,如某行代码为空则提示某行代码错误,如××行代码出现语法错误等错误将提示××行执行错误。(所有提示均在立即窗口显示)

以上代码还有改进余地(如提示为什么错误、语法颜色改变等等功能)

女孩不哭(QQ:191035066)@2011-12-23 22:08:41
http://www.cnblogs.com/nbsofer/archive/2011/12/23/2298788.html

你可能感兴趣的:(字符串)