如何从cmd获得结果

时间:2019-05-30 13:09:13

标签: excel excel-vba macros

我正在尝试使用vba代码打开cmd并执行一些操作,然后得到结果。

这是用于自动化连接到服务器并请求查询并将结果存储在Excel工作表中的过程。

Sub hello()
Dim oShell As Object
Set oShell = CreateObject("Shell.Application")
Dim oOutput As Object
Dim oExec As Object



oShell.ShellExecute "cmd.exe"
Application.Wait (Now + TimeValue("0:00:05"))
SendKeys "plink.exe -ssh username@ipaddress -pw password"
Application.Wait (Now + TimeValue("0:00:05"))
SendKeys "{ENTER}"
Application.Wait (Now + TimeValue("0:00:05"))
SendKeys "{ENTER}"
Application.Wait (Now + TimeValue("0:01:00"))
SendKeys "run sql select n.dnorpattern as Directory_Number, n.description as "
Application.Wait (Now + TimeValue("0:00:05"))
SendKeys "{ENTER}"
Application.Wait (Now + TimeValue("0:00:20"))


Application.Wait (Now + TimeValue("0:00:20"))
SendKeys "%{F4}", True

'handle the results as they are written to and read from the StdOut object
 Dim s As String
 Dim sLine As String
 While Not oOutput.AtEndOfStream
 sLine = oOutput.ReadLine

 If sLine <> "" Then MsgBox sLine
 Wend

我正在尝试此代码以获取输出,但失败

Set oOutput = oShell.StdOut

有什么建议吗? 谢谢

0 个答案:

没有答案