Application.Ontime取消无法对对象“应用程序”的方法“ONTIME”

完全失去了,所以任何帮助将不胜感激。

我正在尝试取消2个打开工作簿时触发的计划事件,并重复使用Application.Ontime方法。

我知道要终止OnTime日程安排循环,您必须提供计划运行的确切时间,并且具有多个Application.OnTime任务需要多个variables。 这就是为什么我设置了两个公共variables(选项显式下面的文档的标题):

Dim dTime as Date Dim dTime2 as Date 

调度程序使用这些variables,一切运行正常,代码每分钟运行一次。

dTime的值在TaskTracker函数中设置为:

 dTime = Now() + TimeValue("00:01:00") Application.OnTime dTime, "TaskTracker", , True 

dTime2的值在Autoclear函数内设置为:

 dTime2 = Now() + TimeValue("00:01:00") Application.OnTime dTime, "AutoClear", , True 

尽pipe如此,当试图在模块的末尾运行函数时,我得到了Object'Application方法的'ONTIME'错误消息:

 Function AutoDeactivate() Application.OnTime EarliestTime:=dTime, Procedure:="TaskTracker", _ Schedule:=False Application.OnTime EarliestTime:=dTime2, Procedure:="AutoClear", _ Schedule:=False End Function 

这是我绝对不会得到什么问题的地方。 触发debugging将我带到每个程序取消尝试的OnTime部分。

下面是包含这些元素的脚本。 希望这会给你们一些洞见,为什么这些事件不能被取消。 请给我这个剧本是漫长的:P

 Option Explicit Dim dTime As Date Dim dTime2 As Date '------------------------------------------------------------ 'This is what checks cells to define if an email notification has to be sent, and what the content of that email should be. '------------------------------------------------------------ Function TaskTracker() Dim FormulaCell As Range Dim FormulaRange As Range Dim NotSentMsg As String Dim MyMsg As String Dim SentMsg As String Dim SendTo As String Dim CCTo As String Dim BCCTo As String Dim MyLimit As Double Dim MyLimit2 As Double dTime = Now() + TimeValue("00:01:00") NotSentMsg = "Not Sent" SentMsg = "Sent" SendTo = ThisWorkbook.Worksheets("Tasks").Range("D2") CCTo = ThisWorkbook.Worksheets("Tasks").Range("E2") BCCTo = ThisWorkbook.Worksheets("Tasks").Range("F2") MyLimit = Date MyLimit2 = ((Round(Now * 1440, 0) - 30) / 1440) Set FormulaRange = ThisWorkbook.Worksheets("Tasks").Range("F5:F35") On Error GoTo EndMacro: For Each FormulaCell In FormulaRange.Cells With FormulaCell If DateValue(CDate(.Value)) = MyLimit Then MyMsg = SentMsg If .Offset(0, 1).Value = NotSentMsg Then strTO = SendTo strCC = CCTo strBCC = BCCTo strSub = "[Task Manager] Reminder that you need to: " & Cells(FormulaCell.Row, "B").Value If Cells(FormulaCell.Row, "C").Value = "" Then strBody = "Greetings, " & vbNewLine & vbNewLine & _ "Your task : " & Cells(FormulaCell.Row, "B").Value & " is nearing its Due Date: " & Cells(FormulaCell.Row, "F").Value & "." & vbNewLine & "A wise decision would be to complete this task before it expires!" & _ vbNewLine & vbNewLine & "Truly yours," & vbNewLine & "Task Manager" Else strBody = "Hello, " & vbNewLine & vbNewLine & _ "Your task : " & Cells(FormulaCell.Row, "B").Value & " with the mention: " & Cells(FormulaCell.Row, "C").Value & " is nearing its Due Date: " & Cells(FormulaCell.Row, "F").Value & "." & vbNewLine & "A wise decision would be to complete this task before it expires!" & _ vbNewLine & vbNewLine & "Truly yours," & vbNewLine & "Task Manager" End If If sendMail(strTO, strSub, strBody, strCC) = True Then MyMsg = SentMsg End If Else MyMsg = NotSentMsg End If If .Value = MyLimit2 Then MyMsg = NotSentMsg End If Application.EnableEvents = False .Offset(0, 1).Value = MyMsg Application.EnableEvents = True End With Next FormulaCell ExitMacro: Exit Function EndMacro: Application.EnableEvents = True MsgBox "Some Error occurred." _ & vbLf & Err.Number _ & vbLf & Err.Description Application.OnTime dTime, "TaskTracker", , True End Function '------------------------------------------------------------ 'This is the function that clears the rows of Completed Tasks '------------------------------------------------------------ Function AutoClear() Dim i As Integer dTime2 = Now() + TimeValue("00:01:00") With Tasks For i = 5 To 35 If .Cells(i, 4).Value Like "Done" And .Cells(i, 5).Value = "1" Then .Cells(i, 1).ClearContents .Cells(i, 2).ClearContents .Cells(i, 3).ClearContents .Cells(i, 5).ClearContents .Cells(i, 6).ClearContents .Cells(i, 4).Value = "Pending" .Cells(i, 7).Value = "Not Sent" End If Next i End With Tasks.AutoFilter.ApplyFilter Application.OnTime dTime2, "AutoClear", , True End Function '------------------------------------------------------------ 'ThisWorkbook calls this to deactivate the Application.OnTime. This "should" prevent the Excel process from reoppening the worksheets. '------------------------------------------------------------ Function AutoDeactivate() On Error Resume Next Application.OnTime EarliestTime:=dTime, Procedure:="TaskTracker", _ Schedule:=False Application.OnTime EarliestTime:=dTime2, Procedure:="AutoClear", _ Schedule:=False End Function 

看起来这是一个设置错误!

 Option Explicit Dim dTime As Date Dim dTime2 As Date Application.OnTime dTime, "TaskTracker", , True Application.OnTime dTime2, "AutoClear", , True 

使用工作簿closures时调用的AutoDeactivation函数按预期工作!

 Function AutoDeactivate() On Error Resume Next Application.OnTime EarliestTime:=dTime, Procedure:="TaskTracker", _ Schedule:=False Application.OnTime EarliestTime:=dTime2, Procedure:="AutoClear", _ Schedule:=False End Function 

Workbook_BeforeClose:

 Private Sub Workbook_BeforeClose(Cancel As Boolean) Call AutoDeactivate End Sub 

发生了什么是相当愚蠢的。 我在工作中遇到了取消事件的问题,所以我将Excel Sheet放在首页并对上面find的修复进行了编码。 然而,它仍然没有工作。 不是因为它有一个错误,而是因为我在家里没有Outlook! :P

没有Outlook应用程序阻止事件在运行一次之后被重新安排(导致自动解除ActiveX错误消息)。

所以只要我把这个脚本回来工作(在Outlook安装),一切工作正常:)

标记为自己解决哈哈。