回答
您可以CreateProcess
應用程序將其StdOut
重定向到管道,然後直接讀取該管道; http://pastebin.com/CszKUpNS
dim resp as string
resp = redirect("cmd","/c dir")
resp = redirect("ipconfig","")
您總是可以將shell輸出重定向到一個文件,然後從該文件讀取輸出。
Sub StdOutTest()
Dim objShell As Object
Dim objWshScriptExec As Object
Dim objStdOut As Object
Dim rline As String
Dim strline As String
Set objShell = CreateObject("WScript.Shell")
Set objWshScriptExec = objShell.Exec("c:\temp\batfile.bat")
Set objStdOut = objWshScriptExec.StdOut
While Not objStdOut.AtEndOfStream
rline = objStdOut.ReadLine
If rline <> "" Then strline = strline & vbCrLf & CStr(Now) & ":" & Chr(9) & rline
' you can handle the results as they are written to and subsequently read from the StdOut object
Wend
MsgBox strline
'batfile.bat
'ping 1.1.1.1 -n 1 -w 2000 > nul
'echo 2
'ping 1.1.1.1 -n 1 -w 2000 > nul
'echo 4
'ping 1.1.1.1 -n 1 -w 2000 > nul
'echo 6
'ping 1.1.1.1 -n 1 -w 2000 > nul
'echo 8
End Sub
基於安德魯·萊薩德的答案,這裏的運行命令並返回輸出字符串的函數 -
Public Function ShellRun(sCmd As String) As String
'Run a shell command, returning the output as a string
Dim oShell As Object
Set oShell = CreateObject("WScript.Shell")
'run command
Dim oExec As Object
Dim oOutput As Object
Set oExec = oShell.Exec(sCmd)
Set oOutput = oExec.StdOut
'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 s = s & sLine & vbCrLf
Wend
ShellRun = s
End Function
用法:
MsgBox ShellRun("dir c:\")
我在最近的[Python文章]中記錄了你的這個很好的答案(http://stackoverflow.com/questions/39516875/return-result-from-python-to-vba/39517658#39517658)。隨意直接回答,我會刪除我自己的。 – Parfait 2016-09-15 18:11:17
我無法使用您的示例獲得此功能。我需要'ShellRun(「cmd.exe/c dir c:\」)。然後它完美地工作。謝謝。 – mal 2016-09-16 03:21:49
你不需要這裏的while循環,你可以用'Set oOutput = oExec.StdOut'這行代替直到函數結束:'ShellRun = oExec.StdOut.ReadAll' – 2017-06-06 20:02:45
基於bburns.km's answer,我添加了傳入輸入t(使用StdInput)在調用期間執行到可執行文件。以防有人絆倒這一點,並有相同的需求。
''' <summary>
''' Executes the given executable in a shell instance and returns the output produced
''' by it. If iStdInput is given, it is passed to the executable during execution.
''' Note: You must make sure to correctly enclose the executable path or any given
''' arguments in quotes if they contain spaces.
''' </summary>
''' <param name="iExecutablePath">
''' The full path to the executable (and its parameters). This string is passed to the
''' shell unaltered, so be sure to enclose paths and parameters containing spaces
''' in quotes (").
''' </param>
''' <param name="iStdInput">
''' The (optional) input to pass to the executable. Default: Null
''' </param>
Public Function ExecuteAndReturnStdOutput(ByVal iExecutablePath As String, _
Optional ByVal iStdInput As String = vbNullString) _
As String
Dim strResult As String
Dim oShell As WshShell
Set oShell = New WshShell
Dim oExec As WshExec
Set oExec = oShell.Exec(iExecutablePath)
If iStdInput <> vbNullString Then
oExec.StdIn.Write iStdInput
oExec.StdIn.Close ' Close input stream to prevent deadlock
End If
strResult = oExec.StdOut.ReadAll
oExec.Terminate
ExecuteAndReturnStdOutput = strResult
End Function
Note: You will need to add a reference to
Windows Script Host Object Model
so the typesWshShell
andWshExec
are known.
(To do this go to Extras ->References in the VBA IDE's menu bar.)
- 1. 如何從shell命令捕獲輸出?
- 2. shell命令捕獲
- 3. 在org模式下使用babel從shell命令捕獲輸出
- 4. Python,從shell命令捕獲整個輸出
- 5. php:捕獲命令輸出
- 6. 捕獲輸出命令CMD
- 7. 在Python中捕獲複雜shell命令的輸出
- 8. 從git命令捕獲輸出?
- 9. 從java捕獲ssh命令輸出
- 10. 在Python中捕獲netcat shell命令輸出
- 11. 簡單的python命令來捕獲shell執行輸出?
- 12. 如何捕獲pry shell命令的輸出?
- 13. 捕獲遠程命令在shell腳本內的變量輸出
- 14. 在ncurses中捕獲命令行輸出
- 15. Python:獲取shell命令的輸出'history'
- 16. 獲取「getprop | grep」shell命令的輸出
- 17. 如何從等待輸入的命令中捕獲命令輸出?
- 18. 如何從Android shell中的PS命令輸出中獲取PID
- 19. NSTextView的Shell命令輸出
- 20. 從wget的輸出shell grep命令
- 21. Bash - 捕獲命令的輸出
- 22. 如何捕獲命令行中的所有命令和輸出?
- 23. 我想從命令獲得輸出到shell中的變量expect
- 24. 從curl命令獲取shell腳本中的正確輸出
- 25. 管道輸出命令shell命令
- 26. 從shell調用python並捕獲輸出
- 27. 如何從CakePHP Shell捕獲輸出
- 28. 如何從捕獲的命令中回顯多行輸出?
- 29. 從Perl數組中指定的命令捕獲輸出
- 30. 捕獲所有命令輸出
哇不可能創造了這個我感謝:) – user310291 2010-05-08 06:51:31
抱歉,你是怎麼讓你的代碼運行?我試圖做類似的事情(將標準輸入數據輸入到VBA中,並且我在OSX上運行),但我不確定在哪裏聲明函數。我試着將它們放在同一個文件夾中,當它們單擊提交時,它定義了我的用戶表單的功能,但它給了我一個錯誤,指出「編譯錯誤:只有註釋可能出現在End Sub,End Function或End Property之後」。 – Engineero 2013-06-12 01:51:05
這是Windows特定的代碼,因爲它使用Windows API;它不會在OSX上運行,無論你做什麼 - 最好問一個新問題。 – 2013-06-12 10:11:16