现有如下代码,如果不将数据传递到text1中,能不能传递到指定的变量中去,怎么传递
’窗体上面有text1    command1控件Private Sub Command1_Click()
  Dim script_control As Object
  Set script_control = CreateObject("MSScriptControl.ScriptControl")
  script_control.Language = "VBScript"
  
   
  script_control.AddObject "text1", Me.Text1
  script_control.AddCode "sub main()" + vbCrLf + "dim a " +vbCrlf+"a=1"+vbCrlf+"text1.text=a"+ vbCrLf + "end sub"
  script_control.Run "Main"
Private Sub Command1_Click()

解决方案 »

  1.   

    Sub Command1_Click()
      Dim script_control As Object
      Set script_control = CreateObject("MSScriptControl.ScriptControl")
      script_control.Language = "VBScript"
        
      script_control.AddObject "Form1", Me '<--这里改一下,用form1对象传递变量;
                                           '下面一行的text1.text改成Form1.Tag
      script_control.AddCode "sub main()" + vbCrLf + "dim a" + vbCrLf + "a=1" + vbCrLf + "Form1.Tag=a" + vbCrLf + "end sub"
      script_control.Run "Main"
      
      Dim S As String
      Dim N As Integer
      
      S = Form1.Tag
      N = Val(Form1.Tag)
      
      Debug.Print S; N
      
    End Sub
      

  2.   

    Sub Command1_Click() 
      Dim script_control As Object 
      Set script_control = CreateObject("MSScriptControl.ScriptControl") 
      script_control.Language = "VBScript" 
        
      script_control.AddObject "Form1", Me                                        script_control.AddCode "sub main()" + vbCrLf + "dim a" + vbCrLf + "a=1" + vbCrLf + "Form1.Tag=a" + vbCrLf + "end sub" 
      script_control.Run "Main" 
      
      Dim S As String 
      Dim N As Integer 
      
      S = Form1.Tag 
      N = Val(Form1.Tag) 
      
      Debug.Print S; N 
      
    End Sub
      

  3.   

    '在form1上做一个TextBox控件数组,只留下Text1(0),然后把它的visible设置为False,专门做为传递变量用。
    '程序如下改一下,有多少变量都可以传出来。
    Sub Command1_Click()  Dim script_control As Object
      Set script_control = CreateObject("MSScriptControl.ScriptControl")
      script_control.Language = "VBScript"
        
      Dim i As Integer
      For i = 1 To 2
          Load Text1(i)
      Next
      For i = 0 To 2
          script_control.AddObject "T" & Trim(i), Me.Text1(i)
      Next
      
      script_control.AddCode "sub main()" + vbCrLf + "dim a:a=1:t0=a:t1=a+1:t2=a+2" + vbCrLf + "end sub"
      script_control.Run "Main"
      
      Dim N(2) As Integer
      For i = 0 To 2
          N(i) = Val(Text1(Trim(i)))
          Debug.Print N(i)
      Next  For i = 1 To 2
          Unload Text1(i)
      NextEnd Sub'不要想那么复杂,实用的就是最好的。