等待 shell 命令完成 [英] Wait for shell command to complete

查看:46
本文介绍了等待 shell 命令完成的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在 Excel VBA 中运行一个简单的 shell 命令,该命令在指定目录中运行一个批处理文件,如下所示:

I'm running a simple shell command in Excel VBA that runs a batch file in a specified directory like below:

Dim strBatchName As String
strBatchName = "C:folder
unbat.bat"
Shell strBatchName

有时批处理文件在某些​​计算机上可能需要更长时间才能运行,并且有依赖于批处理文件的正在进行的 VBA 代码才能完成运行.我知道你可以设置一个等待计时器,如下所示:

Sometimes the batch file might take longer on some computer to run, and there are proceeding VBA code that is dependent on the batch file to finish running. I know you can set a wait timer like below:

Application.Wait Now + TimeSerial(0, 0, 5)

但这可能不适用于某些速度太慢的计算机.有没有办法系统地告诉 Excel 继续执行其余的 VBA 代码,直到 after shell 完成运行?

But that might not work on some computer that are too slow. Is there a way to systematically tell Excel to proceed with the rest of the VBA code until after the shell has finish running?

推荐答案

改用 WScript.Shell,因为它有一个 waitOnReturn 选项:

Use the WScript.Shell instead, because it has a waitOnReturn option:

Dim wsh As Object
Set wsh = VBA.CreateObject("WScript.Shell")
Dim waitOnReturn As Boolean: waitOnReturn = True
Dim windowStyle As Integer: windowStyle = 1

wsh.Run "C:folder
unbat.bat", windowStyle, waitOnReturn

(想法复制自 等待 Shell 完成,然后格式化单元格- 同步执行一个命令)

这篇关于等待 shell 命令完成的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆