vba 声明和调用睡眠 API

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/22325958/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-12 02:16:45  来源:igfitidea点击:

Declaring & Calling The Sleep API

vbams-accessaccess-vbams-access-2010

提问by spcurtis81

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.

我已经阅读了许多关于实现 Sleep API 的线程,但是大多数线程都比我在知识方面领先一步,我将不胜感激。

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.

我在 VBA 中有一个宏,我想在步骤之间添加暂停(纯粹是美观而不是功能),但是我在基本声明和调用上挣扎。

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

我知道首先我必须声明 api 并且这个代码是......

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.

如果有人可以帮助解决这个问题,并感谢您对我的基本掌握的耐心,我将不胜感激。

采纳答案by spcurtis81

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

Sleep 的声明应该放在模块的顶部。标准编码模块。

You can omit Calland just use

您可以省略Call并使用

Sleep 1000  ' 1 second delay

anywhere within an existing sub, so

在现有 sub 内的任何地方,所以

Option Explicit

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

Sub Main()

    ' wait 3 seconds
    Sleep 3000

End Sub

enter image description here

在此处输入图片说明



an alternative without declaring the Sleep sub would be

不声明 Sleep sub 的替代方法是

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