Excel工作簿会反复打开 [英] Excel workbook gets opened repeatedly

查看:134
本文介绍了Excel工作簿会反复打开的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述


我使用Application.ontime()方法来安排一些宏。关闭工作簿后,它会一次又一次地打开。为了克服这个问题,我在工作簿上设置了另一个事件 - BeforeClosed。现在它显示运行时错误1004:方法'OnTime'
'对象'应用程序失败。即使在从网络上获取帮助上下文之后,我也无法理解这种情况。下面是代码。请给我解决方案。

I have used the method Application.ontime() for scheduling some macros.After closing the workbook, it gets opened again and again. to overcome this problem, I set another event on workbook- BeforeClosed. Now it is showing runtime error 1004:Method 'OnTime' of 'Object'_Application failed.I am not getting why this happening even after reffering the help context from web. Below code is given. Please give me solution.

Dim starttime,rtime

私人 Sub Workbook_Open ()

starttime
= 现在 + TimeValue " 00:02:00"

申请
OnTime EarliestTime := starttime 程序 := " startapp" 时间表 :=

rtime
= TimeValue " 14:30:00"

申请
OnTime EarliestTime := rtime 程序 := " sendreminder" 时间表 :=

申请
OnTime EarliestTime := rtime 程序 := " sendreminder_out" 时间表 :=

申请
OnTime EarliestTime := rtime 程序 := " SendReminderFromProxy" 时间表 :=


结束 Sub


私人 Sub Workbook_BeforeClose Cancel as 布尔

MsgBox
"亲爱的" & "" & 环境 " USERNAME" & "," & "请不要忘记在关闭前保存。"

starttime
= 现在 + TimeValue " 00:02:00"

申请
OnTime EarliestTime := starttime 程序 := " startapp" 时间表 := 错误

rtime
= TimeValue " 14:30:00"

申请
OnTime EarliestTime := rtime 程序 := " sendreminder" 时间表 := 错误

申请
OnTime EarliestTime := rtime 程序 := " sendreminder_out" 时间表 := 错误

申请
OnTime EarliestTime := rtime 程序 := " SendReminderFromProxy" 时间表 := 错误

结束 Sub

Private Sub Workbook_Open() starttime = Now + TimeValue("00:02:00") Application.OnTime EarliestTime:=starttime, Procedure:="startapp", schedule:=True rtime = TimeValue("14:30:00") Application.OnTime EarliestTime:=rtime, Procedure:="sendreminder", Schedule:=True Application.OnTime EarliestTime:=rtime, Procedure:="sendreminder_out",Schedule:=True Application.OnTime EarliestTime:=rtime, Procedure:="SendReminderFromProxy",Schedule:=True End Sub Private Sub Workbook_BeforeClose(Cancel As Boolean) MsgBox "Dear" & " " & Environ("USERNAME") & ", " & "Please do not forget to save before closing." starttime = Now + TimeValue("00:02:00") Application.OnTime EarliestTime:=starttime, Procedure:="startapp", Schedule:=False rtime = TimeValue("14:30:00") Application.OnTime EarliestTime:=rtime, Procedure:="sendreminder", Schedule:=False Application.OnTime EarliestTime:=rtime, Procedure:="sendreminder_out", Schedule:=False Application.OnTime EarliestTime:=rtime, Procedure:="SendReminderFromProxy", Schedule:=False End Sub

推荐答案

In首先,您应该使用Workbook_BeforeClose中的现有值  starttime,而不是新值,因为您要停止已经安排的操作。

In the first place, you should use the existing value starttime in Workbook_BeforeClose, not a new value, because you want to stop the already scheduled action.

第二,如果用户开始关闭工作簿,然后取消保存它的提示,计划的操作将被取消。当用户再次开始关闭工作簿时,Workbook_BeforeClose事件将再次运行并导致
错误,因为没有我们必须阻止这种行为。

In the second place, if the user starts to close the workbook, then cancels the prompt to save it, the scheduled actions will have been canceled. When the user starts to close the workbook again, the Workbook_BeforeClose event will run again and cause an error since there are no scheduled actions to cancel anymore. So we have to prevent that.

Private Sub Workbook_BeforeClose(取消为布尔值)

    MsgBox"亲爱的" ;&""& Environ(" USERNAME")&","&"请不要忘记在结束前保存。"
$
     On Error Resume Next

    Application.OnTime EarliestTime: = starttime,Procedure:=" startapp",Schedule:= False

    Application.OnTime EarliestTime:= rtime,Procedure:=" sendreminder",Schedule:= False

    Application.OnTime EarliestTime:= rtime,Procedure:=" sendreminder_out",Schedule:= False

    Application.OnTime EarliestTime:= rtime,Procedure:=" SendReminderFromProxy",Schedule:= False

End Sub

Private Sub Workbook_BeforeClose(Cancel As Boolean)
    MsgBox "Dear" & " " & Environ("USERNAME") & ", " & "Please do not forget to save before closing."
    On Error Resume Next
    Application.OnTime EarliestTime:=starttime, Procedure:="startapp", Schedule:=False
    Application.OnTime EarliestTime:=rtime, Procedure:="sendreminder", Schedule:=False
    Application.OnTime EarliestTime:=rtime, Procedure:="sendreminder_out", Schedule:=False
    Application.OnTime EarliestTime:=rtime, Procedure:="SendReminderFromProxy", Schedule:=False
End Sub


这篇关于Excel工作簿会反复打开的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆