在 VBA 中取消 Application.OnTime 的位置

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/1182028/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-11 10:37:49  来源:igfitidea点击:

Where to cancel Application.OnTime in VBA

vbaexcel-vbaexcel

提问by dymoacon

Using VBA in Excel 2003, I'm trying to cancel an Application.OnTime event using the following code:

在 Excel 2003 中使用 VBA,我尝试使用以下代码取消 Application.OnTime 事件:

Application.OnTime EarliestTime:=varNextRunTime, Procedure:="SomeMethod", Schedule:=False

where varNextRunTime is a global variable containing the next time it is due to run. This code runs in the Workbook_BeforeClose event handler so is always run when the workbook is closed, which is my intention.

其中 varNextRunTime 是包含下一次运行时间的全局变量。此代码在 Workbook_BeforeClose 事件处理程序中运行,因此始终在工作簿关闭时运行,这是我的意图。

However, if the user tries to close the workbook, but changes their mind and hits the cancel button when prompted to Save (Yes, No, Cancel), the Application.OnTime event is still cancelled. BeforeClose is always run before they decide to hit cancel, so has anyone got any ideas how I can only cancel the Application.OnTime event when the workbook is closed?

但是,如果用户尝试关闭工作簿,但在提示保存(是、否、取消)时改变主意并点击取消按钮,则 Application.OnTime 事件仍会被取消。BeforeClose 总是在他们决定取消之前运行,所以有没有人知道如何在工作簿关闭时我只能取消 Application.OnTime 事件?

回答by barrowc

Check the Savedproperty of the Workbookin your event handler. If the workbook is unsaved then display your own dialog to find out if the users wants to save changes, not save changes or cancel.

检查事件处理程序中的Saved属性Workbook。如果工作簿未保存,则显示您自己的对话框以了解用户是否要保存更改、不保存更改或取消。

Here's some rough code. Obviously uncomment the line which deals with the Application.OnTimepart and change the MsgBoxtitle to something suitable

这是一些粗略的代码。显然取消注释处理该Application.OnTime部分的行并将MsgBox标题更改为合适的内容

Private Sub Workbook_BeforeClose(Cancel As Boolean)

Dim response As Integer

If Not (Me.Saved) Then
    response = MsgBox("Do you want to save changes to '" & Me.Name & "'?", vbYesNoCancel, "put title here")

    If (response = vbCancel) Then
        Cancel = True
    ElseIf (response = vbYes) Then
        Me.Save
    End If
End If

If Not (Cancel) Then
    ' Application.OnTime EarliestTime:=varNextRunTime, Procedure:="SomeMethod", Schedule:=False
End If

End Sub

回答by dymoacon

A bit late to the show but here is a simple solution that I've come across (and tested):

节目有点晚了,但这是我遇到(并测试过)的一个简单解决方案:

If a user deactivates the workbook by closing it, the workbook will still remain the ActiveWorkbook when the Workbook_WindowDeactivate event fires. If the user deactivates the workbook by switching to another workbook, then the new workbook will become the ActiveWorkbook by the time Workbook_WindowDeactivate fires. You can use this behavior to determine the action that caused the event to fire:

如果用户通过关闭工作簿来停用它,则当 Workbook_WindowDeactivate 事件触发时,该工作簿仍将保持为 ActiveWorkbook。如果用户通过切换到另一个工作簿来停用该工作簿,则新工作簿将在 Workbook_WindowDeactivate 触发时变为 ActiveWorkbook。您可以使用此行为来确定导致事件触发的操作:

Private Sub Workbook_WindowDeactivate(ByVal Wn As Window)
    If Application.ActiveWorkbook.Name = Me.Name Then
        'Your code here
    End If
End Sub

回答by Smandoli

Investigate using:

调查使用:

Application.Quit

If you find this command results in the Excel program remaining open although the document has closed, you may want to follow with

如果您发现此命令导致 Excel 程序在文档已关闭但仍保持打开状态时,您可能需要遵循

ActiveWorkbook.Close False

I'm not in position to test this or give more insights, unfortunately.

不幸的是,我无法对此进行测试或提供更多见解。