Application.Ontime 取消无法调用对象“Application”的“ONTIME”方法

2024-04-18

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

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

我知道要终止 OnTime 计划循环,您必须提供计划运行的确切时间,并且拥有多个 Application.OnTime 任务需要多个变量。 这就是为什么我设置了两个公共变量(Option Explicit 下面的文档标题):

Dim dTime as Date
Dim dTime2 as Date

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

dTime的值设置在任务追踪器函数为:

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

dTime2的值设置在自动清除函数为:

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

尽管如此,我还是得到了对象“Application”的方法“ONTIME”尝试在模块末尾运行该函数时出现错误消息:

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

这是我绝对不明白出了什么问题的地方。触发调试会将我带到每个过程取消尝试的 OnTime 部分。

下面是包含这些元素的脚本。希望这能让你们了解为什么这些活动不能被取消。

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

工作簿关闭时调用的 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

工作簿_关闭前:

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

发生的事情非常愚蠢。我在取消工作时遇到了问题,因此我将 Excel 表带回家并编写了上面找到的修复程序。然而,它仍然没有起作用。不是因为其中有错误,而是因为我家里没有Outlook! :P

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

因此,当我将此脚本重新投入使用(安装了 Outlook 的地方)时,一切都正常运行:)

将此标记为我自己解决的哈哈。

本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

Application.Ontime 取消无法调用对象“Application”的“ONTIME”方法 的相关文章

  • C# .Net Serial DataReceived 事件响应对于高速数据来说太慢

    我已经设置了一个 SerialDataReceivedEventHandler 并在 VS2008 Express 中使用基于表单的程序 我的串口设置如下 115200 8N1 Dtr 和 Rts 已启用 接收字节阈值 1 我有一个通过蓝牙
  • 如何让VLOOKUP在VBA中选择到最低行?

    希望自动在单元格中插入 VLOOKUP 公式 录制宏时 我指示它使用相同的公式填充下面的列 效果很好 但是 当 VLOOKUP 搜索的表发生变化 更多或更少的行 时 就会出现问题 在记录时 VLOOKUP 下降到表中的最后一行 273 但是
  • 导出到excel时如何显示前导零?

    我正在通过更改内容类型来创建 Excel 报告 Response ContentType application vnd ms excel 我有包含前导零的值 问题是导出到 Excel 时缺少前导零 e g 000123 gt 123 我知
  • Excel FILTER() 对于空白单元格返回 0

    我怀疑以前有人问过这个问题 但我找不到 FILTER 即使指定了返回字符串 通常也会为空白行返回 0 Using filter 我经常收到空单元格的 0 返回值 假设 A 列中有 6 行数据 abc xyz abc xyz abc If I
  • 将包含宏的工作簿复制到不带宏的工作簿

    我能够复制工作簿 复制到所需位置 其中在后台包含宏 该副本还包含相同的宏 我的问题是我不希望这个重复的工作簿包含宏 谁能告诉怎么做吗 先感谢您 将您的工作簿保存为无宏 即简单地保存为 Excel 工作簿 对于我的 Excel 2007 这是
  • 如何等到 Excel 计算公式后再继续 win32com

    我有一个 win32com Python 脚本 它将多个 Excel 文件合并到电子表格中并将其另存为 PDF 现在的工作原理是输出几乎都是 NAME 因为文件是在计算 Excel 文件内容之前输出的 这可能需要一分钟 如何强制工作簿计算值
  • VBA ByRef 参数类型不匹配

    最初在我的主代码部分中 我有一个丑陋的 if 语句 尽管它会运行丑陋 我决定将其设为我要调用的函数 这导致我收到错误 编译错误 ByRef 参数类型不匹配 我的假设是该函数需要正确引用 尽管我一直在阅读文档并且不明白为什么 gt 声明 Sh
  • Excel 数字缩写格式

    这是我想要完成的任务 Value Display 1 1 11 11 111 111 1111 1 11k 11111 11 11k 111111 111 11k 1111111 1 11M 11111111 11 11M 11111111
  • 使用输入作为显示日期的基础

    我需要一种方法来使用用户窗体上的输入来确定将在输出上显示的日期 这是我的代码 If StatusBox Value lt 23 59 And ShiftCode Value AP Then Cells emptyRow 8 Value Da
  • C# 无法将欧元符号打印到文件中(使用 Excel 打开时)

    我在使用 Web api 控制器的 get 方法时遇到问题 此方法返回一个 HttpResponseMessage 对象 该对象具有带有 csv 文件的 HttpContent 其中包含欧元符号 当该方法返回文件时 不会打印欧元符号 该方法
  • SQL Excel VBA 运行时错误 3709 无效连接

    这是我的第一个问题 欢迎提出建设性的批评 我正在尝试从 Excel VBA 查询 Access 数据库并将返回信息放入 Excel 范围中 我收到此错误 错误消息 运行时错误 3709 连接无法用于 执行此操作 在此情况下它已关闭或无效 语
  • 如何添加事件处理程序仅在 sqlalchemy 中提交事务后触发一次

    我正在使用 sqlalchemy 编写一些函数 这些函数被称为内部事务 例如 def create order session arg kw create order object order Order xxxx xxx session
  • 有没有更快的方法来使用Powershell解析Excel文档?

    我正在与一个接口MS Excel文件通过Powershell 每个 Excel 文档可能有大约 1000 行数据 目前这个脚本似乎读取了Excel文件并以每 0 6 秒 1 条记录的速率将值写入屏幕 乍一看 这似乎非常慢 这是我第一次阅读E
  • Cocoa - 捕获 NSStatusItem 鼠标悬停事件

    当用户的鼠标悬停在我的 NSStatusItem 上时 如何执行函数 如果您分配自定义NSView给你的NSStatusItem s view属性 您可以覆盖NSResponder方法mouseEntered mouseMoved and
  • 使用 VBScript 在日期字段值上选择错误的数据

    我有一张包含以下数据的表 现在 Excel 共有 36 个任务 每个任务有 4 列 第一个任务 即 Task1 名称将始终从 L 列开始 144 列描述了 36 个任务 现在我们需要按行进行检查 并需要检查 TNStart 开始日期 你们能
  • 使用PHP从doc、xls文件中读取数据

    我想知道是否可以从 doc 和 xls 文件中读取数据并将 将内容读取到图像文件中 创建文档的页面样本 例如 我有一些文件希望我的客户购买 所以我需要自动创建小图像 例如我的文档样本 我们将不胜感激您的帮助 对于读取 xls 文件 我真的推
  • 如何使用vba复制Excel工作表中的动态范围

    我试图使宏中的范围是动态的 而不指定最后一行x Sheets SheetName Range A2 K1000 Copy在 1000 行中 我想将其更改为动态 因为有时我的数量会更少或更多 尝试这个 Sub Test Dim lRow as
  • 如何将 .xlsx 文件上传到 jenkins 作业

    如何将 xlsx 文件作为构建参数上传到 jenkins 作业 我尝试使用文件参数 但我发现该文件正在丢失其扩展名或原始格式 有什么方法可以从 jenkins UI 将 excel 文件上传到 jenkins 作业吗 In the file
  • 将 MS 转换为秒

    我发现这个公式可以用来将 MS 转换为秒 但它是为 Excel 2002 编写的 而我正在使用 2010 CONCATENATE TEXT INT B1 1000 86400 hh mm ss B1 INT B1 1000 1000 以下是
  • 在 VBA 中捕获 shell 命令的输出值?

    发现这个功能http www cpearson com excel ShellAndWait aspx http www cpearson com excel ShellAndWait aspx 但我还需要捕获 shell 的输出 有什么代

随机推荐