Declaring & Calling The Sleep API

不羁的心 提交于 2019-11-28 07:42:30

问题


I have read many threads on implementing the Sleep API however most are a step ahead of where I am at in terms of knowledge to some guidance would be greatly appreciated.

I have a macro in VBA which I would like to add pauses to, between steps (purely aesthetic rather than functional) however I am struggling with the basic declaring and calling.

I understand that first I must declare the api and that the code for this is...

Private Declare Sub Sleep Lib "kernel32" (ByVal dwMilliseconds As Long)

however, I am confused with regards to where this should go. Should it go in a module of it's own or can it be stated at the top of the macro module?

Then to call the function do I simply add a line to the macro of...

Call Sleep (1000)

I'd appreciate it if anyone can help with this question and appreciate your patience with my basic grasp.


回答1:


The declaration of Sleep should go to the top of a module. Standard coding module.

You can omit Call and just use

Sleep 1000  ' 1 second delay

anywhere within an existing sub, so

Option Explicit

Declare Sub Sleep Lib "kernel32" (ByVal dwMilliseconds As Long)

Sub Main()

    ' wait 3 seconds
    Sleep 3000

End Sub


an alternative without declaring the Sleep sub would be

Application.Wait Now + TimeValue("00:00:03") ' waits 3 seconds


来源:https://stackoverflow.com/questions/22325958/declaring-calling-the-sleep-api

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!