Capturer la valeur de sortie d'une commande shell dans VBA?

trouvé cette fonction http://www.cpearson.com/excel/ShellAndWait.aspx

mais il faudrait aussi capturer la sortie de la sortie shell. Une suggestion de code ?

23
demandé sur Community 2010-05-07 00:46:22

5 réponses

Vous pouvez CreateProcess l'application de rediriger les StdOut pour une pipe, puis lire le tuyau directement; http://pastebin.com/CszKUpNS

dim resp as string 
resp = redirect("cmd","/c dir")
resp = redirect("ipconfig","")
21
répondu Alex K. 2010-05-07 12:06:27

basé sur la réponse D'Andrew Lessard, voici une fonction pour exécuter une commande et retourner la sortie sous forme de chaîne de caractères -

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

Utilisation:

MsgBox ShellRun("dir c:\")
36
répondu Brian Burns 2017-12-20 14:52:15

vous pouvez toujours rediriger la sortie shell vers un fichier, puis lire la sortie du fichier.

5
répondu Lance Roberts 2010-05-06 21:44:48

basé sur bburns.réponse de km , j'ai ajouté passer l'entrée (en utilisant StdInput) à l'exécutable pendant l'appel. Juste au cas où quelqu'un tombe sur ce et a le même besoin.

''' <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 it in quotes if it contains spaces.
''' </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: vous devrez ajouter une référence à Windows Script Host Object Model pour que les types WshShell et WshExec soient connus.

(Pour ce faire, allez à Extras - > References in the VBA's menu bar.)

4
répondu Marcus Mangelsdorf 2018-07-25 17:48:50
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
3
répondu Andrew Lessard 2014-12-10 08:03:54