Tìm thấy chức năng này http://www.cpearson.com/excel/ShellAndWait.aspxCapture giá trị đầu ra từ một lệnh shell trong VBA?
Nhưng cũng cần phải nắm bắt đầu ra từ đầu ra của vỏ. Bất kỳ đề xuất mã nào?
Tìm thấy chức năng này http://www.cpearson.com/excel/ShellAndWait.aspxCapture giá trị đầu ra từ một lệnh shell trong VBA?
Nhưng cũng cần phải nắm bắt đầu ra từ đầu ra của vỏ. Bất kỳ đề xuất mã nào?
Bạn có thể CreateProcess
ứng dụng chuyển hướng StdOut
của nó vào đường ống, sau đó đọc đường ống đó trực tiếp; http://pastebin.com/CszKUpNS
dim resp as string
resp = redirect("cmd","/c dir")
resp = redirect("ipconfig","")
Bạn luôn có thể chuyển hướng kết xuất của trình bao đến tệp, sau đó đọc kết quả đầu ra từ tệp.
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
Dựa trên câu trả lời Andrew Lessard của, đây là một chức năng để chạy một lệnh và trả về kết quả như là một chuỗi -
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
Cách sử dụng:
MsgBox ShellRun("dir c:\")
Tôi đã ghi nhận câu trả lời tuyệt vời này của bạn trên [bài đăng Python] gần đây (http://stackoverflow.com/questions/39516875/return-result-from-python-to-vba/39517658#39517658). Hãy trả lời trực tiếp và tôi sẽ tự xóa nó. – Parfait
Tôi không thể làm việc này bằng ví dụ của bạn. Tôi cần 'ShellRun (" cmd.exe/c dir c: \ ")' thay thế. Sau đó, nó hoạt động hoàn hảo. Cảm ơn bạn. – mal
bạn không cần vòng lặp while ở đây, bạn có thể thay thế từ dòng 'Set oOutput = oExec.StdOut' cho đến khi kết thúc hàm với dòng này: 'ShellRun = oExec.StdOut.ReadAll' –
Dựa trên bburns.km's answer, tôi đã thêm vào inpu t (sử dụng StdInput) để thực thi trong khi gọi. Chỉ trong trường hợp ai đó tình cờ gặp điều này và có cùng nhu cầu.
''' <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 không thể tạo ra này bản thân mình nhờ :) – user310291
Xin lỗi, nhưng làm thế nào bạn có được mã của bạn để chạy? Tôi đang cố gắng để làm một cái gì đó tương tự (kéo dữ liệu stdout vào VBA, và tôi đang chạy trên OSX) và tôi không chắc chắn nơi để khai báo các chức năng của bạn. Tôi đã thử đặt chúng trong cùng một thư mục xác định chức năng của biểu mẫu người dùng khi họ nhấp gửi, nhưng nó đã cho tôi một lỗi ghi "Lỗi biên dịch: Chỉ các nhận xét có thể xuất hiện sau End Sub, End Function hoặc End Property". – Engineero
Đây là mã Windows cụ thể vì nó sử dụng API Windows; nó sẽ không chạy trên OSX bất kể bạn làm gì - tốt hơn để hỏi một câu hỏi mới. –