Use This Function , You don't need WScript.Sleep or Sleep
Windows Vista以降のTimeoutコマンドを使用する
[Windowsのtimeout/sleepコマンドで実行を一時停止する]
(http://www.atmarkit.co.jp/ait/articles/1206/08/news137.html)
Windows Vista/Windows Server 2003以降のWindows OSには、timeout.exeというコマンドが用意されている。ということでこれを使います。
これで秒単位は制御できます。
VBA VBS
WaitForVistaLater(integer)
integer には正の整数をいれてください
Function WaitForVistaLater(i)
'By QiiQ from Qiita
Dim wsh: Set wsh = CreateObject("Wscript.Shell")
wsh.Run "timeout " & int(abs(i)), 0, True
End Function
Powershellでミリセカンド
VBA VBS
WaitForps(integer)
integer には正の整数をいれてください
Function WaitForps(imillisec)
'By QiiQ from Qiita
Dim str, Cmd , i
Dim wsh: Set wsh = CreateObject("Wscript.Shell")
i = int(abs(imillisec))
Cmd = "PowerShell -nologo -Command " & """" & """" & " set-executionpolicy remotesigned -scope process -force;Start-Sleep -milliseconds " & i & ";" & """"""
CreateObject("WScript.Shell").Run Cmd, 0, True
End Function
accuracy 正確性
ただし、たぶんPowershellを起動して戻る分、正確な時間ではないです。
とはいうもののDeclarareとかWscript.Sleepでもめることはこれを使う限りは発生しないです。
正確性は
Sleep or wscript.sleep > WaitForVistaLater > WaitForPs
と体感ではなりました。
このため、秒単位であればWaitForVistaLateを使う意味があります。
可読性 readability; legibility
ただし、2つの関数は
- VBA,VBScriptいずれでも使用できる
- Declareが不要
- このためオフィス64Bit 32Bitを問わない
- XpでもWscript.Shell runとPowershell使えればWaitForPsが使える
- 正確性はWaitforVistaLater,汎用性はWaitForPs
このような長所を持っています。