我想在VBA代码中使用下面的命令行,命令是递归地获取所有.pdf和.docx文件,并在CMD中运行良好。
dir /s *.pdf *.docx
现在在VBA中,有下面的内容,它递归地打印出所有文件,而不是指定的扩展名。
Sub Run()
Debug.Print ShellRun("cmd.exe /c dir c:\ /s *.pdf *.docx")
End Sub
Public Function ShellRun(sCmd As String) As String
Dim oShell As Object
Set oShell = CreateObject("WScript.Shell")
Dim oExec As Object
Dim oOutput As Object
Set oExec = oShell.Exec(sCmd)
ShellRun = oExec.StdOut.ReadAll
End Function
发布于 2020-07-17 12:13:59
尝尝这个
Debug.Print ShellRun("cmd.exe /c dir /s c:\*.pdf c:\*.docx")
编辑:
您需要运行dir /s PATH\*.pdf PATH\*.docx
,在\*
之间没有空间。同时,对于pdf和docx,PATH
应该被重复使用。但是,在您的代码中,您有空格,您还交换了c:\
的位置和/s
标志。
https://stackoverflow.com/questions/62959959
复制