如何将变量中的内容替换出来执行?
例如:
dim str as string
str="frm.show"   'frm是已经存在的窗体请问如何将str中的内容取出来执行谢谢

解决方案 »

  1.   

    看我的:
    [[***************2004.7.8----jimy***********]]
    方法一:(使用WinAPI方法)
    1)引用API
    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
    2)声明API函数
    Private Function ExecuteLine(sCode As String, Optional fCheckOnly As Boolean) As Boolean
        ExecuteLine = EbExecuteLine(StrPtr(sCode), 0&, 0&, Abs(fCheckOnly)) = 0
    End Function
    3)使用API函数实现字符串form的显示(进行简单的封装)
    Private Sub ShowForm(strFrm As String)
        Dim strTest As String
        strTest = "dim f as " & strFrm
        ExecuteLine strTest
        strTest = "set f=new " & strFrm
        ExecuteLine strTest
        strTest = "f.show  "
        ExecuteLine strTest
    End Sub
    Private Sub Command1_Click()
        ShowForm "Form2"
    End Sub
    4)其他命令的转化(注意带字符串参数的函数)
    Private Sub Command2_Click()
        ExecuteLine "dim i as long ,j as long", False
        ExecuteLine "i = 1:j=3", False
        ExecuteLine "msgbox i + j", False
        ExecuteLine "msgbox (1+2)*3/4", False
        ExecuteLine "dim a as string", False
        ExecuteLine "a = ""dd""", False
        ExecuteLine "msgbox a", False
        ExecuteLine "msgbox ""hello"",false"
    End Sub
    注:它不会执行自定义的函数
    如: ExecuteLine "ShowForm",false     'ShowForm为自定义函数方法二:(只能用户于显示form)
    1)定义函数(用于显示form)
    Private Function ShowNamedForm(strForm As String) As Form
        Dim frmTemp As Form
        Set frmTemp = Forms.Add(strForm)
        frmTemp.Show
        Set ShowNamedForm = frmTemp
    End Function
    2)使用
    Private Sub Command3_Click()
        Dim frmtmp As Form
        Set frmtmp = ShowNamedForm("Form2")
        frmtmp.Caption = "hello"  '更改frm的标题
    End Sub
      

  2.   

    hehe,还是冰儿马甲牛
    http://community.csdn.net/Expert/topic/3377/3377875.xml?temp=.5137445