我能够提出运行我的shell程序的VBA代码。 但我发现它并没有等待。 它将转到下一行代码并在shell开始处理文件之前移动文件。
Dim ShellString As String
ShellString = _
"cmd.exe /k " _
+ "cpdf.exe" + _
" -split " + _
Chr(34) + PDFFile + Chr(34) + _
" -o " + _
Chr(34) + ActiveWorkbookPath + "\" + OutputFileSubFolder + "\" + ProcessingFileWithoutExtension + "_%%%.pdf" + Chr(34)
Shell ShellString
' THERE IS SOME CODE HERE THAT MOVES MY FILE.
' THIS CODE RUNS BEFORE THE SHELL COMMAND RUNS
' THAT'S THE PROBLEM. IT MOVES THE FILE BEFORE SHELL HAS A CHANCE TO WORK ON IT...
所以有2个想要的东西。 1)我想从运行的shell命令中捕获错误代码。 2)因此,我想也不做任何其他逻辑,直到返回错误代码....即,在shell完成并返回错误代码之前不要移动到下一行代码...
答案 0 :(得分:1)
VBA' s Shell function是异步的,这是您遇到的问题。相比之下,VBScript的Run method更加灵活。从VBA调用它很容易。在您的代码中添加:
Dim WshShell As Object
Dim ErrorCode As Long
Set WshShell = VBA.CreateObject("WScript.Shell")
ErrorCode = WshShell.Run(ShellString, 1, True)