locked
Application.OnTime not working in a class module RRS feed

  • Question

  • I've created a Class Module to enable Application Events. One of the app event procedures includes this statement:

    Application.OnTime Now + TimeSerial(0, 0, 0.001) "MyProc"

    This works as expected in a standard module, but MyProc is never started when OnTime is run from the Class module. I've also tried putting the OnTime in a standard module that's called from the class module; same issue.

    Is there a way to use the OnTime method from a Class Module?

    Thursday, August 25, 2011 9:00 PM

Answers

  • Woo-hoo! Found it. Thanks to WinDiff...an oldie but a goodie.

    I had recently gone through all my modules and added "Option Private Module" to the headers of those that I thought were safe to do that with, in order to hide their procs from the Macros dialog box.

    Oops.

    Speculation: Application.OnTime is a WordBasic carry-over, and is just as oblivious to procedures in private modules as is the Macros dialog box. And the procedure being called by OnTime was in a newly--and I now know--erroneously, privatized module.

    Likewise--

    Application.OnTime Now + TimeSerial(0, 0, 0.001), "MyProject.MyModule.MySub"

    --is gibberish to OnTime. It quietly fails.

    <sarcasm>And, believe it or not, the Help file is WRONG regarding OnTime syntax!!!</sarcasm>

    OnTime can cope with "MySub", or "MyModule.MySub", because both were valid in WordBasic. But (more speculation) "MyProject.MyModule.MySub" wouldn't have worked in Word 95, so it won't work in Word 2010, either.

    So, with those two changes made, Application.OnTime functionality was restored.

    Whew!

    To Cindy, if she's lurking, (and I bet she is):

    My next stop is to find Application.OnTime in the MSDN Library, where I'll suggest the correction.

    In "Classic Mode".

    • Proposed as answer by Andrei Smolin Saturday, August 27, 2011 7:27 AM
    • Marked as answer by JRV529088 Saturday, August 27, 2011 1:46 PM
    Saturday, August 27, 2011 1:58 AM

All replies

  • Hello,

    I think the problem is in the "MyProc" part - that sort of things is usually required to be located in a standard module.


    Regards from Belarus (GMT + 2),

    Andrei Smolin
    Add-in Express Team Leader
    Thursday, August 25, 2011 9:05 PM
  • Thanks; I should have mentionned that MyProc is in a standard module. Only the OnTime is in the class module.
    Thursday, August 25, 2011 9:07 PM
  • Does using the "Project.Module1.MyProc" format instead of "MyProc" help?
    Regards from Belarus (GMT + 2),

    Andrei Smolin
    Add-in Express Team Leader
    Thursday, August 25, 2011 9:11 PM
  • Thanks, again. In my OP, I was abbreviating for simplicity, but in my code, I'm using the fully-qualified procedure name.

    Also tried it with the procedure name alone, just for grins. Same result.

    Thursday, August 25, 2011 9:13 PM
  • http://support.microsoft.com/kb/209952?
    Regards from Belarus (GMT + 2),

    Andrei Smolin
    Add-in Express Team Leader
    Thursday, August 25, 2011 9:18 PM
  • Thanks; the KB article applies to Word 2000; this is Word 2010SP1.

    The OnTime is in a global .dotm (though not Normal.dotm). ActiveDocument is (obviously!) not the global .dotm. ActiveDocument.AttachedTemplate has a reference set to the global .dotm, although that's probably irrelevant here.

    Further, it works when the OnTime is in a standard module in the same global .dotm. It only fails when the OnTime is in the app events class module. So whatever "by design" limitation was documented for Word 2000 appears to have been changed since 2000.

    BTW, if I call MyProject.MyStdModule.MyProc directly from the event procedure in the class module (not using OnTime), it works.

    [Edit]

    Well...it USED to work from a std module in the global add-in. I only moved it to the app event class module recently, and before I did, it ran every time. But I just moved it back to the std module and it's not working there, now, either! I'm perplexed, but the problem is not that it's in a class module.

    I have some more head-scratching to do, I guess. Next step is to open a backup copy from when it was working and see what's different.

     

    Thursday, August 25, 2011 9:31 PM
  • >ActiveDocument.AttachedTemplate has a reference set to the global .dotm

    Can you check if the issue occurs for a document that has no such reference?


    Regards from Belarus (GMT + 2),

    Andrei Smolin
    Add-in Express Team Leader
    Thursday, August 25, 2011 10:31 PM
  • I guess I can give that a shot, but the attached template has had a reference set to the global template ever since the global was created.
    Friday, August 26, 2011 12:07 AM
  • Well, the reference is not the problem.

    Just opened an old version of the global. Here, the OnTime is in a global proc called from the Document_New event in ActiveDocument.AttachedTemplate, and it works. While the global is old, ActiveDocument.AttachedTemplate is the latest rev, and has a ref to the global--or the Document events wouldn't be able to run the global procs.

    Which proves that OnTime CAN work in this scenario, because it DID, once upon a time...now just need to figure out why it doesn't NOW! Having solved that, the OnTime will likely work within the App event, too.

    Sure wish it would fail with an error. Currently, the global app event proc runs the OnTime statement, and while MyProc never runs, the global procedure that called it quietly moves on. I guess that's one of the hazards of the OnTime background procedure hack.

    But it's significant that it doesn't error out when I call the proc without OnTime, even in the latest version of the global. Without OnTime, it just works(tm). The proc is OK, the call is what's failing.

    Thanks for your help, Andrei. I'm not a whole lot smarter than I was 4.5 hours ago, except for knowing that there's hope!

    Friday, August 26, 2011 2:19 AM
  • Haven't learned a lot by comparing new and old versions, except how far I've come<g>.

    So, trying to find out what isn't working in the current global template.

    Only procedure that precedes the one in which the OnTime is located is the Document_New in ActiveDocument.AttachedTemplate, which merely calls the global DocNew proc in which the OnTime is located.

    Timing is not the problem; I've tried OnTime timeouts ranging from Now to Now + "00:01:00". Long after the doc is fully loaded and initialized.

    I've stuffed MyProc with debug.print's hoping to find if ANY part of it is running. It is not.

    I've tried making this statement--

        Application.OnTime Now + TimeSerial(0, 0, 0.001), "Project.Module.Test"

    --the first in the proc called by Document_New in ActiveDocument.AttachedTemplate, where the Test proc is a "Hello World" message box.  No go.

    Likewise the "real" OnTime promoted to first, where it SHOULD promptly error out because its prereqs are not yet satisified. No go.

    I've tried making the test OnTime the ONLY statement in the proc called by the global proc that Document_New runs. No go.

    I've tried running the "real" and the test OnTime statement from the Immediate pane. No go.

    Fearing a corrupt .DOTM, I copied all the forms and modules from the global .DOTM to a new .DOTM. No go. (Except the file shrunk from 660K to 450K...hmmm...maybe forgot some Building Blocks?)

    Moved the OnTime statement to ActiveDocument.AttachedTemplate. No go.

    Even rebooted my computer...still no go.

    Advice welcome, but suspect I just need to keep looking. Sure worked well when it was working!

    Friday, August 26, 2011 5:26 AM
  • Woo-hoo! Found it. Thanks to WinDiff...an oldie but a goodie.

    I had recently gone through all my modules and added "Option Private Module" to the headers of those that I thought were safe to do that with, in order to hide their procs from the Macros dialog box.

    Oops.

    Speculation: Application.OnTime is a WordBasic carry-over, and is just as oblivious to procedures in private modules as is the Macros dialog box. And the procedure being called by OnTime was in a newly--and I now know--erroneously, privatized module.

    Likewise--

    Application.OnTime Now + TimeSerial(0, 0, 0.001), "MyProject.MyModule.MySub"

    --is gibberish to OnTime. It quietly fails.

    <sarcasm>And, believe it or not, the Help file is WRONG regarding OnTime syntax!!!</sarcasm>

    OnTime can cope with "MySub", or "MyModule.MySub", because both were valid in WordBasic. But (more speculation) "MyProject.MyModule.MySub" wouldn't have worked in Word 95, so it won't work in Word 2010, either.

    So, with those two changes made, Application.OnTime functionality was restored.

    Whew!

    To Cindy, if she's lurking, (and I bet she is):

    My next stop is to find Application.OnTime in the MSDN Library, where I'll suggest the correction.

    In "Classic Mode".

    • Proposed as answer by Andrei Smolin Saturday, August 27, 2011 7:27 AM
    • Marked as answer by JRV529088 Saturday, August 27, 2011 1:46 PM
    Saturday, August 27, 2011 1:58 AM
  • Congratulations and many thanks!
    Regards from Belarus (GMT + 2),

    Andrei Smolin
    Add-in Express Team Leader
    Saturday, August 27, 2011 7:27 AM