将在VBA中执行的Shell命令的输出管道传输到特定的Shell [英] Piping the output of a Shell Command executed in VBA to a Specific Shell
问题描述
我对VBA还是很陌生,几天前就开始学习它.现在,我试图创建一个宏以执行Shell命令,并将输出通过管道传递到特定工作表中的特定单元格.我要完成的工作是将目录结构的文本转储到工作表中.下面是我到目前为止的代码.
I am still very very new to VBA and started learning it a couple days ago. Now I am trying to create a Macro to execute a shell command and pipe the output to a specific cell in a specific worksheet. What I am trying to accomplish is to get a text dump of the directory structure into a worksheet. Below is the code i have so far.
Sub CopyList()
Call Shell("cmd.exe /S /K" & "dir /s /b directoryPath", vbNormalFocus)
End Sub
执行此宏将弹出命令提示符,并将目录结构转储到cmd窗口内.我想知道如何将其发送到工作表.您的帮助将不胜感激.
Executing this macro brings up a command prompt and dumps the directory structure inside the cmd window. I was wondering how I could pipe this to a worksheet. Your help would be greatly appreciated.
推荐答案
您可以创建WScript.Shell对象并直接读取StdOut:
You can create the WScript.Shell object and read the StdOut directly:
Sub SO()
Range("A1").Value = CreateObject("WScript.Shell").Exec("CMD /S /C dir /s /b directoryPath").StdOut.ReadAll
End Sub
这篇关于将在VBA中执行的Shell命令的输出管道传输到特定的Shell的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!