当在“安全模式"下打开另一个工作簿时,Application.ontime失败. [英] Application.ontime fails when another workbook is open in "safe mode"

查看:80
本文介绍了当在“安全模式"下打开另一个工作簿时,Application.ontime失败.的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在这里扯头发...

I'm ripping my hair out here ...

我有一个Excel工作表,用于记录在不同任务上使用的时间.在工作表中,我启动一个时钟",该时钟在执行任务时一直运行,以10秒为增量.只要没有打开处于安全模式的其他工作簿(即从Web位置打开,并且未激活启用编辑"),此方法就可以正常工作.如果是这种情况,计时器将失败并显示...

I have an Excel worksheet, that I use for registering time used on different tasks. In the worksheet, I start a "clock" that runs as long as I'm working on a task, in increments of 10 seconds. This works just fine, as long as there is not another workbook open, that is in safe-mode (I.e. 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 ...

  • 如果处于安全模式的工作簿是最后一个具有焦点的工作簿",则在转到计算机上的另一个窗口之前,宏将失败.
  • 如果包含宏的工作簿是最后一个具有焦点的Excel窗口,则在转到计算机上的另一个窗口之前,即使还有另一个在安全模式"下打开的工作簿,该宏也不会失败.
  • 如果另一个工作簿是具有焦点的最后一个excel",但是在启用编辑"模式下打开,则宏不会失败.

我的代码:

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.

有人吗?

推荐答案

尝试在"Ontime"行之前键入以下代码:

Try to type this code before the "Ontime" line:

错误恢复时继续
ThisWorkbook.Application.OnTime Now()+ TimeValue("00:00:10"),"Timer"

这避免了意外的行为.

最好

这篇关于当在“安全模式"下打开另一个工作簿时,Application.ontime失败.的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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