ppt使用vba编写倒计时
For those of us who have our minds deeply into VB.NET, the journey back to VB6 can be a confusing trip. Using a Timer in VB6 is like that. At the same time, adding timed processes to your code is not obvious to new users of VBA Macros.
对于那些对VB.NET有深入了解的人来说,回到VB6的旅程可能会令人困惑。 在VB6中使用计时器就是这样。 同时,对于VBA宏的新用户来说,向代码中添加定时进程并不明显。
新手计时器 ( Timers For Newbies )
Coding a Word VBA macro to automatically time a test that was written in Word is a typical reason for using a timer. Another common reason is to see just how much time is being taken by different parts of your code so you can work on optimizing the slow sections. Sometimes, you might want to see if anything is happening in the application when the computer seems to be just sitting there idle, which can be a security problem. Timers can do that.
编码一个Word VBA宏自动时间写在Word中的测试是使用计时器的典型原因。 另一个常见的原因是查看代码的不同部分占用了多少时间,以便您可以优化慢速段。 有时,当计算机似乎只是闲置时,您可能想查看应用程序中是否发生了任何事情,这可能是一个安全问题。 计时器可以做到这一点。
启动计时器 ( Start a Timer )
You start a timer by coding an OnTime statement. This statement is implemented in Word and Excel, but it has different syntax depending on which one you're using. The syntax for Word is:
您可以通过编码OnTime语句来启动计时器。 该语句在Word和Excel中实现,但是根据您所使用的语法,它具有不同的语法。 Word的语法为:
expression.OnTime(When, Name, Tolerance)
expression.OnTime(时间,名称,公差)
The syntax for Excel looks like this:
Excel的语法如下所示:
expression.OnTime(EarliestTime, Procedure, LatestTime, Schedule)
expression.OnTime(最早时间,过程,最新时间,时间表)
Both have the first and second parameter in common. The second parameter is the name of another macro that runs when the time in the first parameter is reached. In effect, coding this statement is like creating an event subroutine in VB6 or VB.NET terms. The event is reaching the time in the first parameter. The event subroutine is the second parameter.
两者都有共同的第一个和第二个参数。 第二个参数是到达第一个参数中的时间时运行的另一个宏的名称。 实际上,对该语句进行编码就像使用VB6或VB.NET术语创建事件子例程一样。 事件已达到第一个参数中的时间。 事件子例程是第二个参数。
This is different from the way it is coded in VB6 or VB.NET. For one thing, the macro named in the second parameter can be in any code that is accessible. In a Word document, Microsoft recommends putting it in the Normal document template. If you put it in another module, Microsoft recommends using the full path: Project.Module.Macro.
这与VB6或VB.NET中的编码方式不同。 一方面,第二个参数中命名的宏可以位于任何可访问的代码中。 在Word文档中,Microsoft建议将其放在“普通”文档模板中。 如果将其放在另一个模块中,Microsoft建议使用完整路径:Project.Module.Macro。
The expression is usually the Application object. The Word and Excel documentation states that the third parameter can cancel the execution of the event macro in case a dialog or some other process prevents it from running within a certain time. In Excel, you can schedule a new time in case that happens.
表达式通常是Application对象。 Word和Excel文档指出,如果对话框或其他进程阻止它在一定时间内运行,则第三个参数可以取消事件宏的执行。 在Excel中,您可以安排新的时间以防万一。
编码时间事件宏 ( Code the Time Event Macro )
This code in Word is for the administrator who wants to display a notification that the testing time has expired and print the result of the test.
Word中的此代码适用于希望显示测试时间已到期并打印测试结果的通知的管理员。
Public Sub TestOnTime()Debug.Print "The alarm will go off in 10 seconds!"Debug.Print ("Before OnTime: " & Now)alertTime = Now + TimeValue("00:00:10")Application.OnTime alertTime, "EventMacro"Debug.Print ("After OnTime: " & Now)End SubSub EventMacro()Debug.Print ("Executing Event Macro: " & Now)End Sub
Public Sub TestOnTime()Debug.Print“警报将在10秒后发出!” Debug.Print(“ OnTime之前:”&Now)alertTime = Now + TimeValue(“ 00:00:10”)Application.OnTime alertTime, “ EventMacro” Debug.Print(“ OnTime:”&Now)结束子SubEventMacro()Debug.Print(“ Executing Event Macro:”&Now)
This results in the following content in the immediate window:
这将在立即窗口中产生以下内容:
The alarm will go off in 10 seconds!Before OnTime: 12/25/2000 7:41:23 PMAfter OnTime: 12/25/2000 7:41:23 PMExecuting Event Macro: 2/27/2010 7:41:33 PM
警报将在10秒内响起!在OnTime之前:12/25/2000 7:41:23 PM在OnTime之后:12/25/2000 7:41:23 PM执行事件宏:2/27/2010 7:41:33 PM
其他Office应用程序的选项 ( Option for Other Office Apps )
Other Office applications don't implement OnTime. For those, you have several choices. First, you can use the Timer function, which simply returns the number of seconds since midnight on your PC, and does your own math, or you can use Windows API calls. Using Windows API calls has the advantage of being more precise than Timer. Here's a routine suggested by Microsoft that does the trick:
其他Office应用程序不实现OnTime。 对于这些,您有几种选择。 首先,您可以使用Timer函数,该函数仅返回自午夜以来PC上的秒数并进行自己的数学运算,或者可以使用Windows API调用。 使用Windows API调用的优点是比Timer更精确。 这是Microsoft建议的执行此操作的例程:
Private Declare Function getFrequency Lib "kernel32" _Alias "QueryPerformanceFrequency" (cyFrequency As Currency) As LongPrivate Declare Function getTickCount Lib "kernel32" _Alias "QueryPerformanceCounter" (cyTickCount As Currency) As LongSub TestTimeAPICalls()Dim dTime As DoubledTime = MicroTimerDim StartTime As SingleStartTime = TimerFor i = 1 To 10000000Dim j As Doublej = Sqr(i)NextDebug.Print ("MicroTimer Time taken was: " & MicroTimer - dTime)End SubFunction MicroTimer() As Double'' Returns seconds.'Dim cyTicks1 As CurrencyStatic cyFrequency As Currency'MicroTimer = 0' Get frequency.If cyFrequency = 0 Then getFrequency cyFrequency' Get ticks.getTickCount cyTicks1' SecondsIf cyFrequency Then MicroTimer = cyTicks1 / cyFrequencyEnd Function
私有声明函数getFrequency Lib“ kernel32” _Alias“ QueryPerformanceFrequency”(cyFrequency作为货币)作为LongPrivate声明函数getTickCount Lib“ kernel32” _Alias“ QueryPerformanceCounter”(cyTickCount作为货币)作为LongSub TestTimeTimeTimeTimer CallTime(DimdTime) TimerFor i = 1到10000000Dim j作为Doublej = Sqr(i)NextDebug.Print(“ MicroTimer花费的时间为:“&MicroTimer-dTime)End子函数MicroTimer()为Double”返回秒。 'MicroTimer = 0'获取频率。如果cyFrequency = 0则getFrequency cyFrequency'获取ticks.getTickCount cyTicks1'SecondsIf cyFrequency然后MicroTimer = cyTicks1 / cyFrequencyEnd函数
翻译自: https://www.thoughtco.com/timer-in-office-vba-macros-3424056
ppt使用vba编写倒计时
本文介绍了如何在Office VBA宏中使用计时器,特别是针对PPT的倒计时功能。通过编码OnTime语句启动计时器,以及在Word和Excel中的不同语法。此外,还提供了在其他Office应用程序中实现类似功能的选项,如使用Timer函数或Windows API调用。


被折叠的 条评论
为什么被折叠?



