Delay macro to allow events to finish

2019-09-21 06:43发布

While attempting to access an external API function set from within a macro I found it necessary to add in a delay to allow time for the external API to process a selection.

Implementing this caused a bit of difficulty as using Application.Wait or Application.Sleep wouldn't work.

Searching online led me to try using GetTickCount or timeGetTime - both of which failed to work for me as the delay function failed to exit.

The question thus is how can I implement this?

1条回答
该账号已被封号
2楼-- · 2019-09-21 07:22

Instead I implemented the delay using the functions Now and DateAdd:

Function Delay(Seconds As Long)
    Dim StopTime As Date: StopTime = DateAdd("s", Seconds, Now)
    Do While Now < StopTime
        DoEvents
    Loop
End Function

Updated below for millisecond precision

Function Delay(Seconds As Single)
    Dim StopTime As Single: StopTime = Timer + Seconds
    Do While Timer < StopTime
        DoEvents
    Loop
End Function
查看更多
登录 后发表回答