简体   繁体   English

在“安全模式”下打开另一个工作簿时,Application.ontime 失败

[英]Application.ontime fails when another workbook is open in “safe mode”

I'm ripping my hair out here ...我在这里扯头发...

I have an Excel worksheet, that I use for registering time used on different tasks.我有一个 Excel 工作表,用于记录不同任务的时间。 In the worksheet, I start a "clock" that runs as long as I'm working on a task, in increments of 10 seconds.在工作表中,我启动了一个“时钟”,只要我在处理任务,它就会以 10 秒的增量运行。 This works just fine, as long as there is not another workbook open, that is in safe-mode (Ie opened from a weblocation, and "enable editing" not activated).这工作得很好,只要没有打开另一个工作簿,即处于安全模式(即从网络位置打开,并且“启用编辑”未激活)。 If this is the case, the timer fails with...如果是这种情况,计时器将失败...

"Run-time error '1004':
Method 'OnTime' of object '_Application' failed.

AND...和...

  • If the workbook in safemode is the "last workbook to have focus", before going to another window on the computer, the macro will fail.如果安全模式下的工作簿是“最后一个获得焦点的工作簿”,则在转到计算机上的另一个窗口之前,宏将失败。
  • If the workbook that contains the macro is the last Excel window to have focus, before going to another window on the computer, even if there is also another workbook open in "safemode", the macro does NOT fail.如果包含宏的工作簿是最后一个获得焦点的 Excel 窗口,则在转到计算机上的另一个窗口之前,即使在“安全模式”下还有另一个工作簿打开,宏也不会失败。
  • If another workbook is the "last excel to have focus", but is open in "edit enabled" mode, the macro will NOT fail.如果另一个工作簿是“最后一个获得焦点的 excel”,但在“启用编辑”模式下打开,则宏不会失败。

My code:我的代码:

Dim TimerActive As Boolean

Sub Start_Timer()
    TimerActive = True
    Timer
End Sub

Sub Stop_Timer()
    TimerActive = False
End Sub

Sub Timer()
    If TimerActive Then
        ThisWorkbook.Sheets("Register_time").Range("InputTimeTo").Value = Time
        ThisWorkbook.Application.OnTime Now() + TimeValue("00:00:10"), "Timer"
    End If
End Sub

When the macro fails, the debugger highlights the line:当宏失败时,调试器会突出显示该行:

ThisWorkbook.Application.OnTime Now() + TimeValue("00:00:10"), "Timer"

I would like to somehow work around this, so that the macro works "in the background", even if another workbook has focus (safemode or not), or if a completely different program has focus.我想以某种方式解决这个问题,以便宏在“后台”工作,即使另一个工作簿具有焦点(安全模式与否),或者如果一个完全不同的程序具有焦点。

Note: I can't have the workbook with the macro gain focus, when the clock needs to update, as this would mean that I would leave the program that I'm working in. It would, however, be ok, if the clock was not updated, until I choose to set focus back to the worksheet where the clock is displayed.注意:当时钟需要更新时,我无法使用具有宏增益焦点的工作簿,因为这意味着我将离开我正在使用的程序。但是,如果时钟没有更新,直到我选择将焦点设置回显示时钟的工作表。

Anyone?有人吗?

Try to type this code before the "Ontime" line:尝试在“Ontime”行之前键入此代码:

On Error Resume Next出错时继续下一步
ThisWorkbook.Application.OnTime Now() + TimeValue("00:00:10"), "Timer" ThisWorkbook.Application.OnTime Now() + TimeValue("00:00:10"), "计时器"

This avoid the unexpected behavior.这避免了意外行为。

Best,最好的,

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM