VBA Timer类(VBA Timer Class)

2019-10-23 22:31发布

所以我试图写在VBA定时器类,并且一切似乎都很好,除了一两件事。

当我打电话Application.OnTime我被要求提供经常性功能的名称和功能,坐在我的定时器类,因此我不知道如何访问它里面。

我曾尝试通过定时器实例名到定时器从中调用该函数如下:

Application.OnTime pNextTick, pName & ".RestartInterval"

其中PNAME是外面的计时器实例的名称:

Set AutoUpdater = New C_Timer
AutoUpdater.Name = "AutoUpdater"

但无论我做什么,我收到以下错误:

不能运行宏“blablablablablabla.xlsm‘!AutoUpdater.RestartInterval’。宏可能无法在此工作簿是可用还是所有宏可能会被禁用。

宏没有我的情况下禁用的,所以我在这里需要一些帮助...

这里是我的C_Timer类到目前为止:

 'test module Public Sub test() Set AutoUpdater = New C_Timer AutoUpdater.Name = "AutoUpdater" AutoUpdater.Interval = "00.00.5" AutoUpdater.WhatToRun = "DoSomething" AutoUpdater.StartTimer End Sub Function DoSomething() MsgBox "sdoiigsligsdgoidjh" End Function 'C_Timer class Private pWhatToRun As String Private pInterval As String Private pName As String Private pRunning As Boolean Private pNextTick Private Function RestartInterval() If pWhatToRun <> "" Then Application.Run pWhatToRun pNextTick = Now + TimeValue(pInterval) Application.OnTime pNextTick, pName & ".RestartInterval" End If End Function Public Function StartTimer() As Boolean On Error GoTo hell If TimeValue(pInterval) > TimeValue("00.00.00") And pName <> "" And pRunning <> True Then pNextTick = Now + TimeValue(pInterval) Application.OnTime pNextTick, pName & ".RestartInterval" pRunning = True Else GoTo hell End If Exit Function hell: pRunning = False M_Settings.SetStatus "Failed to update" End Function Public Function StopTimer() As Boolean On Error GoTo hell If pRunning = True Then Application.OnTime pNextTick, "RestartInterval", , False pRunning = False Else GoTo hell End If Exit Function hell: End Function Public Property Get WhatToRun() As String WhatToRun = pWhatToRun End Property Public Property Let WhatToRun(Value As String) pWhatToRun = Value End Property Public Property Get Interval() As String Interval = pInterval End Property Public Property Let Interval(Value As String) pInterval = Value End Property Public Property Get Name() As String Name = pName End Property Public Property Let Name(Value As String) pName = Value End Property 

更新:我结束了使用下面的答案。 这里是我完整的代码,如果有人想在将来使用它:

 'M_Factory module Public Function CreateTimer(name As String, interval As String) As C_Timer Set newTimer_ = New C_Timer newTimer_.name = name newTimer_.interval = interval Set CreateTimer = newTimer_ End Function 'C_Timer Class Private pInterval As String Private pName As String Private pRunning As Boolean Private pNextTick Public Function Process(func) If func <> "" Then Application.Run func pNextTick = Now + TimeValue(pInterval) Application.OnTime pNextTick, pName & "_Tick" End If End Function Public Function StartTimer() As Boolean On Error GoTo hell If TimeValue(pInterval) > TimeValue("00.00.00") And pName <> "" And pRunning <> True Then pNextTick = Now + TimeValue(pInterval) Application.OnTime pNextTick, pName & "_Tick" pRunning = True Else GoTo hell End If Exit Function hell: pRunning = False M_Settings.SetStatus "Failed to update, close & reopen the document" End Function Public Property Get Start() As C_Timer StartTimer Set Start = Me End Property Public Function StopTimer() As Boolean On Error GoTo hell If pRunning = True Then Application.OnTime pNextTick, pName & "_Tick", , False pRunning = False Else GoTo hell End If Exit Function hell: End Function Public Property Get interval() As String interval = pInterval End Property Public Property Let interval(Value As String) pInterval = Value End Property Public Property Get name() As String name = pName End Property Public Property Let name(Value As String) pName = Value End Property 'test module Public Timer1 As C_Timer, Timer2 As C_Timer Public Sub test() Set Timer1 = M_Factory.CreateTimer("Timer1", "00.00.01").Start Set Timer2 = M_Factory.CreateTimer("Timer2", "00.00.5").Start End Sub Public Function Timer1_Tick() Timer1.Process "DoSomething" End Function Public Function Timer2_Tick() Timer2.Process "DoMoreStuff" End Function Public Sub stopit() Timer1.StopTimer Timer2.StopTimer End Sub Function DoSomething() Sheets(1).Cells(1, 1).Value = Format(DateTime.Now, "HH:NN:SS") End Function Function DoMoreStuff() Sheets(1).Cells(1, 2).Value = Format(DateTime.Now, "HH:NN:SS") End Function 

Answer 1:

要运行的程序的名称OnTime也许应该是一个公共Substandard module 。 你试过以下?

标准模块

Set AutoUpdater = New C_Timer

Public Sub TriggerUpdater()
    AutoUpdater.InsideMyTimerClass
End Sub

类C_Timer

Application.OnTime pNextTick, "TriggerUpdater"

Public Function InsideMyTimerClass() as Variant
...
End Function


文章来源: VBA Timer Class