从字符串执行VB代码

本文介绍在VB5或VB6环境中,如何使用Microsoft脚本控制组件从文本框中的字符串执行VB代码。通过添加Microsoft脚本控制到项目组件中,可以实现对文本框内容的动态执行,示例代码展示了具体操作步骤。
摘要由CSDN通过智能技术生成

在VB5或VB6中,要从字符串(例如,文本框)执行VB代码,请添加

P rojectMicrosoft脚本控制 | C 0 mponents。

这是一些示例代码,显示了一种使用它的方法...

Private Sub Form_Load()
  Text1.Text = "cmdText1.Visible = False"
  Call ScriptControl1.AddObject(cmdText1.Name, cmdText1)
End Sub 
Private Sub Command1_Click()
  Call ScriptControl1.ExecuteStatement(Text1.Text)
End Sub
通过在TheScripts或整个Web上搜索“ Microsoft脚本控件”,可以获得更多信息。 作者: 社区>新闻组存档> comp.lang。*> Visual Basic>如何执行文本框的内容的 Steve Gerrard(来宾)

Phaneendra Varma引起了小组的注意

From: https://bytes.com/topic/visual-basic/insights/641537-execute-vb-code-string

首先:Imports System.Runtime.InteropServices 然后: 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 ' <DllImport("VBA6.DLL", EntryPoint:="EbExecuteLine", SetlastError:=True, CharSet:=CharSet.Unicode, ExactSpelling:=True, CallingConvention:=CallingConvention.StdCall)> Public Shared Function EbExecuteLine(ByVal pStringToExec As Long, ByVal Unknownn1 As Long, ByVal Unknownn2 As Long, ByVal fCheckOnly As Long) As Long '<DllImport("D:\vba6.dll", CharSet:=CharSet.Ansi, SetlastError:=True)> Public Shared Function EbExecuteLine(ByVal pStringToExec As Long, ByVal Unknownn1 As Long, ByVal Unknownn2 As Long, ByVal fCheckOnly As Long) As Long 'End Function '<DllImport("vba6.dll", CharSet:=CharSet.Ansi, SetlastError:=True)> 'Public Shared Function EbExecuteLine(ByVal pStringToExec As Long, ByVal Unknownn1 As Long, ByVal Unknownn2 As Long, ByVal fCheckOnly As Long) As Long 'End Function '定义StrPtr函数,实现获取字符串地址功能 Private Function StrPtr(ByVal e As Object) As Int32 Dim GC As System.Runtime.InteropServices.GCHandle = System.Runtime.InteropServices.GCHandle.Alloc(e, System.Runtime.InteropServices.GCHandleType.Pinned) Dim GC2 As Int32 = GC.AddrOfPinnedObject.ToInt32 GC.Free() Return GC2 End Function Function ExecuteLine(ByVal sCode As String, Optional ByVal fCheckOnly As Boolean = True) As Boolean ExecuteLine = EbExecuteLine(StrPtr(sCode), 0, 0, 0) End Function Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click Dim dm() As String Dim i As Long dm = Split(TextBox.Text, ";") 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
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值