0
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

VBAのSleepとVBSCriptのWscript.Sleepの共用関数Use This Function , You don't need WScript.Sleep or Sleep

Last updated at Posted at 2017-02-12

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 には正の整数をいれてください

WaitForVistaLater.vbs
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 には正の整数をいれてください

WaitForPs.vbs
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つの関数は

  1. VBA,VBScriptいずれでも使用できる
  2. Declareが不要
  3. このためオフィス64Bit 32Bitを問わない
  4. XpでもWscript.Shell runとPowershell使えればWaitForPsが使える
  5. 正確性はWaitforVistaLater,汎用性はWaitForPs

このような長所を持っています。

0
1
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?