2017-06-16 24 views
1

VBAプロジェクト(VBAの初心者の一種)のExcelでWindows APIタイマーを使用していて、Excelがクラッシュするこの質問を考えた。TimerProcがExcelのVBAで完了するたびに他のサブジョブを実行する(Excel VBAのAPI SetTimer)

いくつかのサブプログラムを持つマクロでは、最初のサブプログラムはTimerProcコードを実行するタイマー、たとえば5秒ごとを開始します。タイマーを開始した後、最初のサブが終了し、次のタイマーが始まります。 2番目のサブ関数では他の関数が呼び出され、5秒前にプログラムが終了します。技術的には、TimerProcが呼ばれるまでにマクロ全体が完了します。

この時点で、は、TimerProcコードの終了後に他のサブまたは関数の他の部分を実行/呼び出すことは可能ですか?明らかに、TimerProcコード中に電話をかけることはできますが、それが終わったらどうしたらいいですか?たとえば、TimerProcでKillTimerを実行しても、TimerProcでタイマーを再起動しないと、タイマーを別の場所で再起動する方法はありますか?

注:タイマーは、現在10に設定されている

Public Declare Function SetTimer Lib "user32" (_ 
    ByVal HWnd As Long, _ 
    ByVal nIDEvent As Long, _ 
    ByVal uElapse As Long, _ 
    ByVal lpTimerFunc As Long) As Long 

Public Declare Function KillTimer Lib "user32" (_ 
    ByVal HWnd As Long, _ 
    ByVal nIDEvent As Long) As Long 

Public TimerID As Long 
Public TimerSeconds As Single 

Sub StartTimer() 
    TimerSeconds = 10000 ' how often to "pop" the timer, in milliseconds 
    TimerID = SetTimer(0&, 0&, TimerSeconds, AddressOf TimerProc) 
End Sub 

Sub EndTimer() 
    'On Error Resume Next 
    KillTimer 0&, TimerID 
End Sub 

Sub TimerProc(ByVal HWnd As Long, ByVal uMsg As Long, _ 
     ByVal nIDEvent As Long, ByVal dwTimer As Long) 

    timerFinished = True 
    Call EndTimer 
    'Instead of calling StartTimer here inside TimerProc, I want to exit TimerProc and re-run the below sub, which will now call StartTimer 

End Sub 

サブ

Sub exampleSub() 

    Call StartTimer 

    If timerFinished = True Then 
     Msgbox "It worked!" 
     call StartTimer 
    End If   
End Sub 

:私は、Windowsのタイマーがhttp://www.cpearson.com/excel/OnTime.aspx

未遂例で詳述呼び出しの方法を使用しています秒、そして例のSubは10秒のヒット数よりも前に実行を終了し、TimerProcは実行され、timerFinished boolをTrueに変更します。一度TimerProcが終了すると、がTimerProc自体でStartTimeを呼び出すことなく、のタイマを再起動するために、exampleSubを再実行します(これでtimerFinishedはTrueになります)。私はTimerProcが完全に終了することを確認したい。 TimerProc`が終了した後exampleSubを起動するには

答えて

関連する問題