返回列表 发帖

[问题求助] 如何将VBS代码放在另一个VBS代码中并执行?

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

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

' test1.vbs
WScript.Echo "hello world"
Call GetIP
FUNCTION GetIP
    ' test2.vbs
    set IPConfigSet = GetObject("winmgmts:{impersonationLevel=impersonate}").ExecQuery _
            ("select IPAddress from Win32_NetworkAdapterConfiguration where IPEnabled=TRUE")
    For Each IPConfig IN IPConfigSet
        If Not IsNull(IPConfig.IPAddress) Then
            For i=LBound(IPConfig.IPAddress) To UBound(IPConfig.IPAddress)
                WScript.Echo IPConfig.IPAddress(i)
            Next
        End If
    Next
END FUNCTIONCOPY
我帮忙写的代码不需要付钱。如果一定要给,请在微信群或QQ群发给大家吧。
【微信公众号、微信群、QQ群】http://bbs.bathome.net/thread-3473-1-1.html
【支持批处理之家,加入VIP会员!】http://bbs.bathome.net/thread-67716-1-1.html

TOP

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

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

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

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

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

评分人数

    • lqh123108: 太感谢了,另外能不能放在一个文件中?技术 + 1
『千江有水千江月』千江有水,月映千江;万里无云,万里青天。    http://yu2n.qiniudn.com/

TOP

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

TOP

多谢了,仔细研究下

TOP

返回列表