Diese Funktion gefunden http://www.cpearson.com/excel/ShellAndWait.aspxCapture-Ausgabewert von einem Shell-Befehl in VBA?
Aber müsste auch die Ausgabe von der Shell-Ausgabe erfassen. Irgendein Codevorschlag?
Diese Funktion gefunden http://www.cpearson.com/excel/ShellAndWait.aspxCapture-Ausgabewert von einem Shell-Befehl in VBA?
Aber müsste auch die Ausgabe von der Shell-Ausgabe erfassen. Irgendein Codevorschlag?
Sie können CreateProcess
die Anwendung umleiten seine StdOut
zu einer Rohrleitung, dann lesen Sie diese Rohrleitung direkt; http://pastebin.com/CszKUpNS
dim resp as string
resp = redirect("cmd","/c dir")
resp = redirect("ipconfig","")
Sie könnten die Shell-Ausgabe immer in eine Datei umleiten und dann die Ausgabe aus der Datei lesen.
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
Basierend auf Andrew Lessard Antwort, hier ist eine Funktion einen Befehl und gibt die Ausgabe als String zu laufen -
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
Verbrauch:
MsgBox ShellRun("dir c:\")
Ich habe diese großartige Antwort von Ihnen auf einem kürzlichen [Python post] (http://stackoverflow.com/questions/39516875/return-result-from-python-to-vba/39517658#39517658) gutgeschrieben. Fühlen Sie sich frei, es direkt zu beantworten und ich werde meine eigenen löschen. – Parfait
Ich konnte das nicht mit Ihrem Beispiel arbeiten. Ich brauchte stattdessen ShellRun ("cmd.exe/c dir c: \") '. Dann hat es perfekt funktioniert. Vielen Dank. – mal
Sie brauchen die while-Schleife hier nicht, Sie können von der Zeile 'Set oOutput = oExec.StdOut' bis zum Ende der Funktion mit dieser Zeile ersetzen:' ShellRun = oExec.StdOut.ReadAll' –
Basiert auf bburns.km's answer, fügte ich passing inpu hinzu t (mit StdInput) zu der ausführbaren Datei während des Aufrufs. Für den Fall, dass jemand darauf stolpert und das gleiche Bedürfnis hat.
''' <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.)
Wow nicht diese selbst konnte dank erstellt :) – user310291
Sorry, aber wie Sie Ihren Code haben zu laufen? Ich versuche, etwas Ähnliches zu tun (stdout Daten in VBA, und ich bin auf OSX) und ich bin nicht sicher, wo Sie Ihre Funktionen zu deklarieren. Ich habe versucht, sie in denselben Ordner zu legen, der die Funktionalität meiner Benutzerformulare definiert, wenn sie auf "Senden" klicken, aber es gab eine Fehlermeldung mit dem Hinweis "Kompilierfehler: Nur Kommentare können nach End-Sub, End-Funktion oder End-Eigenschaft erscheinen." – Engineero
Dies ist Windows-spezifischer Code, da er die Windows-API verwendet. Es wird nicht auf OSX laufen, egal was Sie tun - besser, um eine neue Frage zu stellen. –