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”方法 的相关文章

  • VBA:访问 JSON

    我正在处理 VBA 投影 但不确定如何访问此 JSON 中的 id 应该将 players 设置为什么才能在循环中获取 id 我已经用更多代码更新了问题 JSON event games players id 182759 Code Pri
  • Python:使用 python 运行 Excel 宏

    我需要通过 python 运行 Excel 宏 但总是收到以下错误 result self oleobj InvokeTypes dispid LCID wFlags retType argTypes args pywintypes com
  • 为什么 Excel 有时会在工作表名称中添加 $?

    我有时但并非总是发现 Excel 会放置一个 位于工作表名称末尾 但在 Excel 中看不到 只有在尝试使用 C 将其导入 SQL Server 时才可见 我遇到过很多不同的情况 它保留了原始工作表 但也创建了第二个空的 隐藏 工作表 其中
  • 在 VBA 循环中导出查询以根据字符串值选择数据

    我有一个名为 TEST 的表 下面的代码根据 Territory 列中的唯一值循环导出查询 该代码应该根据 Territory 列中的唯一值将数据导出到 Excel 文件 因此每个 Territory 值都有它自己的文件 我在设置 sql
  • 双击事件 - 多个范围

    我正在寻找为双击事件在多个范围内进行编码的最佳方法 Private Sub Worksheet BeforeDoubleClick ByVal Target As Range Cancel As Boolean If Not Interse
  • 通过文本自动创建到另一个工作表的超链接

    我想知道如何基于各自工作表中两个单元格具有的相同文本值 通过脚本自动创建从一个 Excel 工作表到另一个 Excel 工作表的超链接 如果这可以在没有脚本的情况下完成 使用某种公式 如 VLOOKUP 这将是更好的选择 谢谢你的时间 使用
  • Excel Q - 带有二维数组的 SUMIFS

    我有一个二维数组 水平轴上的日期和垂直轴上的标识号 我想要以特定日期和 ID 为条件的总和 并且我想知道如何使用 SUMIFS 来执行此操作 由于某种原因 我似乎不能 因为数组是二维的 而标准范围是一维的 谁能给我关于我可以使用的其他公式的
  • Excel VBA - 循环文件夹中的文件、复制范围、粘贴到此工作簿中

    我有 500 个包含数据的 Excel 文件 我会将所有这些数据合并到一个文件中 实现此目标的任务列表 我想循环遍历文件夹中的所有文件 打开文件 复制此范围 B3 I102 将其粘贴到活动工作簿的第一张工作表中 重复但在下面粘贴新数据 我已
  • 为什么 Rust 在运行时检查数组边界,而(大多数)其他检查发生在编译时?

    正在阅读基本介绍 http doc rust lang org book arrays vectors and slices html 如果您尝试使用不在数组中的下标 您将收到错误 数组访问在运行时进行边界检查 为什么 Rust 在运行时检
  • 检查所选单元格是否在特定范围内

    我正在使用 C 创建 Excel 加载项 如何检查选定的 或代码中范围表示的单元格 是否在特定范围内 例如如何检查单元格 P 5 是否在 A 1 Z 10 范围内 Use Application Intersect 像这样 在VBA中 Su
  • WPF MVVM 在窗口关闭时调用 ViewModel Save 方法

    我已经弄清楚如何从我的 ViewModel 关闭窗口 现在我需要从另一侧解决窗口关闭问题 当用户单击窗口的关闭按钮时 我需要在 ViewModel 中触发 Save 方法 我正在考虑将 Command 属性绑定到 Window 的关闭事件
  • 在 PowerPoint 中查找文本并替换为 Excel 单元格中的文本

    我正在尝试查找 PowerPoint 幻灯片中的单词列表并将其替换为 Excel 文件中单元格中的值 我在 PowerPoint 中运行 VBA 但出现此错误 运行时错误 2147024809 80070057 指定的值超出范围 代码似乎停
  • 依赖注入系统中的事件朝哪个方向发展?

    上或下 我是一个非常注重视觉的人 我将我的应用程序视为一个层次结构 顶部是根 底部是叶子 我还了解到 在 DI 系统中 容器不知道其所包含对象的职责 功能 相反 所包含的对象知道它们的上下文 因为上下文 依赖项 被注入 UP 非 DI 方式
  • 将字段重新格式化为列,其他字段(与先前结构中成为列的字段配对)成为新列中的字段

    我的任务是清理慈善机构设计的移动应用程序中的数据 在一个部分中 用户问答应用程序使用会话由一行表示 该部分由重复的问题答案字段对组成 其中一个字段代表所提出的问题 然后它旁边的字段代表相应的答案 每个问题 字段和答案列对一起代表一个独特的问
  • 有没有办法将 Excel 单元格条目转换为一致的日期和时间格式?

    我正在处理雨量计数据记录器生成的 csv 文件中的一些雨量计数据 我发现日期和时间的记录不一致 以以下两种格式之一交替显示 Format 1 mm dd yyyy hh mm 24 hour clock or Format 2 mm dd
  • VBA仅清除数据透视表缓存,但保留数据透视表结构

    如何使用VBA清除数据透视表缓存 但不破坏数据透视表结构 我的数据透视表已连接到外部数据源 SQL 源决定哪个用户应该查看数据的哪一部分 当表刷新时 源会填充该表 我想保存 Excel 文件并使用干净的数据透视表 内部没有数据 分发它 结果
  • 如何每次使用按钮将数据添加到 MATLAB 中的现有 XLSX 文件?

    我有一个函数可以生成一些变量 例如分数 对 错 未回答 使用按钮调用此功能 问题是如何每次将函数生成的这些值添加 附加到 XLSX 文件中 或者 如何创建 MAT 文件以便可以添加它 可能的解决方案是什么 附加到 xls 文件所涉及的挑战是
  • 使用 Jquery Easyui 将数据网格导出到 Excel

    我是 json 新手 我使用 php 从 mysql 表生成了 jason 数据 并希望将生成的 json 导出为 xls 格式 考试导出 php
  • 使用 Cucumber Scenario Outline 处理 Excel 电子表格

    如果可能的话 我试图找到一种更优雅的方法来处理从与 Excel 电子表格行 第 n 个 相关的 Cucumber Scenario Outline 中调用第 n 个数字 目前 我正在使用迭代编号来定义要从中提取数据的 Excel 电子表格的
  • Android 方向传感器的替代品是什么?

    大家好 我正在为 Android 构建 3D 游戏 我目前正在尝试在我的游戏中添加一个传感器 允许玩家倾斜机器人作为其控制 理想情况下 我想使用方向传感器 但我注意到它已被弃用 有谁知道如何检测 Android 中的倾斜并且不使用这个传感器

随机推荐