我正在尝试像qtp一样编写一个脚本
Public Function sayhi
msgbox "hi"
end
Dim level0
dim count1
count1 = DataTable.GetSheet("Action1").GetRowCount
msgBox count1
For counterVariable = 1 to count1
functionname = DataTable.value("methodnames","Action1")
call functionname
DataTable.GetSheet("Action1").SetCurrentRow(counterVariable)
Next
假设functionname将具有一个值“sayhi”。我可以使用该值来调用该函数吗?就像我在代码“call functionname”中所做的那样。
我知道它不起作用但是怎么做这样的电话?
答案 0 :(得分:12)
使用GetRef()获取Sub或Function的'指针'/引用:
Option Explicit
Sub S1( s )
WScript.Echo "S1:", GetRef( "F1" )( s )
End Sub
Function F1( s )
F1 = UCase( s )
End Function
Dim sName : sName = "S1"
Dim subS1 : Set subS1 = GetRef( sName )
subS1 "abc"
输出:
cscript getrefdemo.vbs
S1: ABC
答案 1 :(得分:6)
Option Explicit
function abc(a)
MsgBox a
End function
dim run : run = "abc ""Hallo"""
execute run
执行方法可以执行此操作
Public Function sayhi
msgbox "hi"
end
Dim level0
dim count1
count1 = DataTable.GetSheet("Action1").GetRowCount
msgBox count1
For counterVariable = 1 to count1
functionname = "call " & DataTable.value("methodnames","Action1")
execute functionname
DataTable.GetSheet("Action1").SetCurrentRow(counterVariable)
Next
如果它在数据表中,将调用sayhi。
答案 2 :(得分:0)
有人提到VBS中没有Include,但是您可以使用...编写代码
Sub Include(yourFile)
Dim oFSO, oFileBeingReadIn ' define Objects
Dim sFileContents ' define Strings
Set oFSO = CreateObject("Scripting.FileSystemObject")
Set oFileBeingReadIn = oFSO.OpenTextFile(yourFile & ".vbs", 1)
sFileContents = oFileBeingReadIn.ReadAll
oFileBeingReadIn.Close
ExecuteGlobal sFileContents
End Sub
我的来源:http://cyreath.blogspot.com/2014/02/vbscript-call-function-in-another-file.html