2010-05-06 51 views

回答

19

您可以CreateProcess應用程序將其StdOut重定向到管道,然後直接讀取該管道; http://pastebin.com/CszKUpNS

dim resp as string 
resp = redirect("cmd","/c dir") 
resp = redirect("ipconfig","") 
+0

哇不可能創造了這個我感謝:) – user310291 2010-05-08 06:51:31

+0

抱歉,你是怎麼讓你的代碼運行?我試圖做類似的事情(將標準輸入數據輸入到VBA中,並且我在OSX上運行),但我不確定在哪裏聲明函數。我試着將它們放在同一個文件夾中,當它們單擊提交時,它定義了我的用戶表單的功能,但它給了我一個錯誤,指出「編譯錯誤:只有註釋可能出現在End Sub,End Function或End Property之後」。 – Engineero 2013-06-12 01:51:05

+1

這是Windows特定的代碼,因爲它使用Windows API;它不會在OSX上運行,無論你做什麼 - 最好問一個新問題。 – 2013-06-12 10:11:16

4

您總是可以將shell輸出重定向到一個文件,然後從該文件讀取輸出。

3
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 
25

基於安德魯·萊薩德的答案,這裏的運行命令並返回輸出字符串的函數 -

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:\") 
+1

我在最近的[Python文章]中記錄了你的這個很好的答案(http://stackoverflow.com/questions/39516875/return-result-from-python-to-vba/39517658#39517658)。隨意直接回答,我會刪除我自己的。 – Parfait 2016-09-15 18:11:17

+0

我無法使用您的示例獲得此功能。我需要'ShellRun(「cmd.exe/c dir c:\」)。然後它完美地工作。謝謝。 – mal 2016-09-16 03:21:49

+4

你不需要這裏的while循環,你可以用'Set oOutput = oExec.StdOut'這行代替直到函數結束:'ShellRun = oExec.StdOut.ReadAll' – 2017-06-06 20:02:45

2

基於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 types WshShell and WshExec are known.
(To do this go to Extras ->References in the VBA IDE's menu bar.)