ppt使用vba编写倒计时_在Office VBA宏中使用计时器
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編寫倒計時
總結
以上是生活随笔為你收集整理的ppt使用vba编写倒计时_在Office VBA宏中使用计时器的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 关于set的自定义比较函数的使用及结构体
- 下一篇: NYOJ82-迷宫寻宝1