excel vba Application.OnTime

2019-08-26 10:15发布

问题:

I asked a question about this last week here I posted my code as requested but no answer. Anyway, I have some more specific questions.

When resetting the timer, what is the purpose of the Procedure argument? It doesn't make sense to have a call-back with Schedule:=False. What happens if the call-back function is no longer in scope for example? Is the timer still reset properly? Is this why most of the examples I've seen (including Chip Pearson - unfortunately, for some reason I'm not allowed to include the link) proceed the OnTime call with On Error Resume Next And, sorry to ask again, but why is this done?

My understanding is that the the timer is registered by the EarliestTime argument and that this is the "serial number" for the timer that uniquely identifies it.
Is the Procedure argument also included in the registration and subsequent identification process as well?

What happens if the call-back procedure is contained in an object that no longer exists when the timer fires? Or more generally, what happens if there is some error when the timer fires and tries to execute the call-back procedure?

If the LatestTime argument is included, what happens after that time if the timer has not been able to fire? Does Windows erase the timer completely?

回答1:

My understanding is that the the timer is registered by the EarliestTime argument and that this is the "serial number" for the timer that uniquely identifies it.

Not sure where you read this? Not that it's a smart thing to do, but you could in principle schedule two or more OnTime runs with the same EarliestTime. So that doesn't make it unique. The combination of EarliestTime and Procedure makes it unique.

When resetting the timer, what is the purpose of the Procedure argument?

Not sure what you mean by "resetting" the timer; do you mean calling OnTime with Schedule := True or Schedule := False?

Either way, how else is it supposed to know which procedure to run (or which procedure run to cancel) if you don't specify the Procedure argument? That's what it's for.

I suggest you try it out and convince yourself.

What happens if the call-back procedure is contained in an object that no longer exists when the timer fires? Or more generally, what happens if there is some error when the timer fires and tries to execute the call-back procedure?

Well, have you tried it to find out for yourself? Nothing mysterious happens. You get an error message as usual.

If the LatestTime argument is included, what happens after that time if the timer has not been able to fire? Does Windows erase the timer completely?

I guess you could put it that way. The procedure won't be run. See VBA help:

For example, if LatestTime is set to EarliestTime + 30 and Microsoft Excel is not in Ready, Copy, Cut, or Find mode at EarliestTime because another procedure is running, Microsoft Excel will wait 30 seconds for the first procedure to complete. If Microsoft Excel is not in Ready mode within 30 seconds, the procedure won’t be run.