我有2个vbs文件。
A.vbs
:
Class test
public a
public b
End Class
B.vbs
:
Dim objShell
Set objShell = Wscript.CreateObject("WScript.Shell")
objShell.Run "C:\Users\shanmugavel.chinnago\Desktop\Test3.vbs"
Dim ins
Set ins = new test 'Here throws "Class not defined: test"
ins.a = 10
ins.b = "SCS"
msgbox ins.a
msgbox ins.b
现在我想在B.vbs文件中实现这一点。但是在为A.vbs中可用的类创建实例时会抛出错误。有什么帮助吗?
答案 0 :(得分:4)
。运行 n。a .vbs不会使代码在另一个代码中可用。一个简单但可扩展的策略是在'libraries'上使用.ExecuteGlobal。给定
Lib.vbs:
' Lib.vbs - simple VBScript library/module
' use
' ExecuteGlobal goFS.OpenTextFile(<PathTo\Lib.vbs>).ReadAll()
' to 'include' Lib.vbs in you main script
Class ToBeAShamedOf
Public a
Public b
End Class ' ToBeAShamedOf
和main.vbs:
' main.vbs - demo use of library/module Lib.vbs
' Globals
Dim gsLibDir : gsLibDir = ".\"
Dim goFS : Set goFS = CreateObject("Scripting.FileSystemObject")
' LibraryInclude
ExecuteGlobal goFS.OpenTextFile(goFS.BuildPath(gsLibDir, "Lib.vbs")).ReadAll()
WScript.Quit main()
Function main()
Dim o : Set o = New ToBeAShamedOf
o.a = 4711
o.b = "whatever"
WScript.Echo o.a, o.b
main = 1 ' can't call this a success
End Function ' main
你会得到:
cscript main.vbs
4711 whatever
(参见this answer了解有用类的种子)
答案 1 :(得分:2)
您的b脚本没有与您的脚本联系,您需要包含这样的代码,然后您可以使用b中存在的代码
call Include("a.vbs")
Sub Include (Scriptnaam)
Dim oFile
Set oFile = oFso.OpenTextFile(Scriptnaam)
ExecuteGlobal oFile.ReadAll()
oFile.Close
End Sub
答案 2 :(得分:1)
您可以将B.vbs转换为Windows Script File,这样您就可以包含A.vbs。
答案 3 :(得分:1)
这是我们用来执行此操作的代码。
Sub Include(sInstFile)
Dim f, s, oFSO
Set oFSO = CreateObject("Scripting.FileSystemObject")
On Error Resume Next
If oFSO.FileExists(sInstFile) Then
Set f = oFSO.OpenTextFile(sInstFile)
s = f.ReadAll
f.Close
ExecuteGlobal s
End If
On Error Goto 0
Set f = Nothing
Set oFSO = Nothing
End Sub
Include("c:\files\SSDConnection.vbs")
Include("c:\files\SSDTable.vbs")
为我们的团队工作完美无缺