Board logo

标题: [问题求助] 如何将VBS代码放在另一个VBS代码中并执行? [打印本页]

作者: lqh123108    时间: 2014-7-23 15:01     标题: 如何将VBS代码放在另一个VBS代码中并执行?

  1. Private Sub Form_Load() '范列
  2. Dim vbs As Object
  3. Set vbs = CreateObject("ScriptControl")
  4. vbs.Language = "vbs"
  5. vbs.AddObject "ThisForm", Me, True
  6. vbs.AddCode "sub mysub():ThisForm.caption=""hello"":end sub"
  7. vbs.ExecuteStatement "mysub"
  8. Set vbs = Nothing
  9. End Sub
复制代码
这是一段VB自动生成VBS,并调用VBS执行的代码..
我现在想..能否将一段VBS代码自动编成上面这段代码的样子,,即把现成的VBS代码自动放在另一个VBS代码中,并执行它..
主要是省得放两个VBS脚本,不采用调用执行的办法)

例如如何将下面代码编成上面的范列:
最好是实现自动化生成上面的范列■
  1. set IPConfigSet = GetObject("winmgmts:{impersonationLevel=impersonate}").ExecQuery _
  2.         ("select IPAddress from Win32_NetworkAdapterConfiguration where IPEnabled=TRUE")
  3. For Each IPConfig IN IPConfigSet
  4.     If Not IsNull(IPConfig.IPAddress) Then
  5.         For i=LBound(IPConfig.IPAddress) To UBound(IPConfig.IPAddress)
  6.             WScript.Echo IPConfig.IPAddress(i)
  7.         Next
  8.     End If
  9. Next
复制代码

作者: Batcher    时间: 2014-7-23 15:40

  1. ' test1.vbs
  2. WScript.Echo "hello world"
  3. Call GetIP
  4. FUNCTION GetIP
  5.     ' test2.vbs
  6.     set IPConfigSet = GetObject("winmgmts:{impersonationLevel=impersonate}").ExecQuery _
  7.             ("select IPAddress from Win32_NetworkAdapterConfiguration where IPEnabled=TRUE")
  8.     For Each IPConfig IN IPConfigSet
  9.         If Not IsNull(IPConfig.IPAddress) Then
  10.             For i=LBound(IPConfig.IPAddress) To UBound(IPConfig.IPAddress)
  11.                 WScript.Echo IPConfig.IPAddress(i)
  12.             Next
  13.         End If
  14.     Next
  15. END FUNCTION
复制代码

作者: yu2n    时间: 2014-7-24 00:39

本帖最后由 yu2n 于 2014-7-24 00:43 编辑

VBS动态加载VBS?使用 Execute 函数。

下面的例子,运行 TestExecute.vbs 后,将调用 GetIPAddress.vbs 中的代码,并且获取 GetIPAddress.vbs 中定义的 strIPAddress 变量的值。

--------------------------------------

GetIPAddress.vbs  (GetIPAddress.vbs代码需要保存为Unicode/ULE编码)
  1. ' GetIPAddress.vbs
  2. set IPConfigSet = GetObject("winmgmts:{impersonationLevel=impersonate}").ExecQuery _
  3.         ("select IPAddress from Win32_NetworkAdapterConfiguration where IPEnabled=TRUE")
  4. For Each IPConfig IN IPConfigSet
  5.     If Not IsNull(IPConfig.IPAddress) Then
  6.         For i=LBound(IPConfig.IPAddress) To UBound(IPConfig.IPAddress)
  7.             rem WScript.Echo IPConfig.IPAddress(i)
  8.             strIPAddress = strIPAddress & IPConfig.IPAddress(i) & vbCrLf
  9.         Next
  10.     End If
  11. Next
复制代码
TestExecute.vbs
  1. ' TestExecute.vbs
  2. ' 读取GetIPAddress.vbs中的所有代码
  3. Dim fso, strCode
  4. Set fso = CreateObject("scripting.filesystemobject")
  5. strCode = fso.OpenTextFile("GetIPAddress.vbs", 1, False, True).ReadAll
  6. ' 运行读取的代码
  7. Execute strCode
  8. ' 获取代码运行后的值
  9. Msgbox "GetIPAddress.vbs 里面的 strIPAddress 变量为:" & vbCrLf & vbCrLf & strIPAddress
复制代码

作者: CrLf    时间: 2014-7-24 00:55

动态调用可以用 Execute 或者 ExecuteGlobal,两者的区别貌似仅在作用域,"ScriptControl" 和 Execute 一样,都不是全局的
话说有这样的需求用 hta 或者 wsf 也不错
作者: lqh123108    时间: 2014-8-6 09:02

多谢了,仔细研究下




欢迎光临 批处理之家 (http://bbs.bathome.net/) Powered by Discuz! 7.2