Excel VBA到QTP 11脚本转换问题

时间:2014-01-20 18:11:34

标签: excel shell excel-vba qtp vba

我已经在Excel VBA中编写了脚本并且工作正常,现在我试图将其转换为QTP 11.0 - 但是面临一些问题。


根据Xiaofu的建议更新了问题 -

QTP 11中的VBA - shell功能是什么?

以下是 QTP 11脚本

Extern.Declare micHwnd, "OpenProcess", "kernel32", "OpenProcess", micLong, micLong, micLong
Extern.Declare micHwnd, "WaitForSingleObject", "kernel32", "WaitForSingleObject", micLong, micLong
Extern.Declare micHwnd, "CloseHandle", "kernel32", "CloseHandle", micLong

Const PROCESS_QUERY_INFORMATION = &H400
Const SYNCHRONIZE = &H100000
Const SWP_NOMOVE = 2
Const SWP_NOSIZE = 1
Const HWND_TOPMOST = -1
Const HWND_NOTOPMOST = -2
Const STATUS_PENDING = &H103
Const STILL_ACTIVE = &H103
Const WAIT_TIMEOUT = &H102
Const INFINITE = &HFFFFFFFF


Public Function test_exe()

Dim argStr

    argStr = "cmd.exe /c " + Chr(34) & "plink.exe -load " + Chr(34) + session_name + Chr(34) + " -l " + Chr(34) + login_id + Chr(34) + " -pw " + Chr(34) + dns_pwd + Chr(34) + " -m " + Chr(34) + cmd_dir + "commands.txt" + Chr(34) + " >> " + Chr(34) + log_dir & log_filename + Chr(34) + Chr(34)
    exeCount = Run_Test(argStr, log_dir + log_filename)

End Function


Public Function Run_Test(exeStr, ByVal logFile)
Dim pid, ExitEvent
Dim lineStr
Dim okFlg
Dim hProcess

exeCount = "0"
okFlg = 0

pid = shell(exeStr, vbHide)

hProcess = Extern.OpenProcess(PROCESS_QUERY_INFORMATION + SYNCHRONIZE, 0, pid)
ExitEvent = Extern.WaitForSingleObject(hProcess, 15000)
Extern.CloseHandle(hProcess)

Set fso = CreateObject("Scripting.FileSystemObject")
Set f = fso.OpenTextFile(logFile, ForWriting, True)
Do
    f.writeLine lineStr
    If InStr(1, lineStr, "/home/") > 0 Then okFlg = okFlg + 1
    exeCount = "1"
Loop Until EOF(3)
f.close

If okFlg >= 1 Then
    Run_Test = okFlg
Else
    Run_Test = -1
End If

End Function

我得到的错误在线#34; **pid = shell(exeStr, vbHide)**" - " 对象不支持此属性或方法:' shell' "

有关如何解决此问题的任何建议吗?

1 个答案:

答案 0 :(得分:1)

我建议您使用DotNetFactory来访问.NET的所有优点:

Dim csProcess
Set csProcess = DotNetFactory.CreateInstance("System.Diagnostics.Process")
Dim myProcess
Set myProcess = csProcess.Start("notepad.exe")
MsgBox myProcess.Id

如果您不熟悉或无法访问MSDN文档,请参阅命令行参数示例:

Dim SystemProcess
Set SystemProcess = DotNetFactory.CreateInstance("System.Diagnostics.Process")

Dim processStartInfo
Set processStartInfo = DotNetFactory.CreateInstance("System.Diagnostics.ProcessStartInfo")

processStartInfo.FileName = "cmd.exe"
processStartInfo.Arguments = "/c notepad.exe"
Dim myProcess
Set myProcess = SystemProcess.Start(processStartInfo)
msgbox myProcess.Id