Consulting

Results 1 to 8 of 8

Thread: Application.OnTime with arguments (totally stumped)

  1. #1
    Microsoft Word MVP 2003-2009 VBAX Guru gmaxey's Avatar
    Joined
    Sep 2005
    Posts
    3,340
    Location

    Application.OnTime with arguments (totally stumped)

    I am trying to call a procedure that take arguments using Application.OnTime.

    I'm almost positive that I've had success with this before, but now I simply can't get it to work. I'm trying to use the syntax outlined here: http://www.markrowlinson.co.uk/articles.php?id=10

    In a standard module I have this code to run tests. Tests 1-3 run fine (the called procedures don't take arguments). However, with Test 4-5 nothing happens.

    I'm sitting here surrounded with piles of hair and bloody scalp! What am I missing/doing wrong? Thanks
    Option Explicit
    Sub Test1()
      Application.OnTime When:=Now + 0.000005, Name:="Testing1"
    End Sub
    Sub Test2()
      Application.OnTime When:=Now + 0.000005, Name:="modMain.Testing2"
    End Sub
    Sub Test3()
      Application.OnTime When:=Now + 0.000005, Name:="Project.modMain.Testing3"
    End Sub
    Sub Test4()
      Application.OnTime When:=Now + 0.000005, Name:="'Testing4 ""Test 4""'"
    End Sub
    Sub Test5()
      Application.OnTime When:=Now + 0.000005, Name:="'modMain.Testing4 ""Test 4""'"
    End Sub
    Sub Test6()
      Application.OnTime When:=Now + 0.000005, Name:="'Project.modMain.Testing4 ""Test 4""'"
    End Sub
    In another standard module named modMain, I have these procedures:

    Option Explicit
    Sub Testing1()
      MsgBox "Test 1"
    End Sub
    Sub Testing2()
      MsgBox "Test 2"
    End Sub
    Sub Testing3()
      MsgBox "Test 3"
    End Sub
    Sub Testing4(strTest As String)
      MsgBox strTest
    End Sub
    Greg

    Visit my website: http://gregmaxey.com

  2. #2
    VBAX Expert Dave's Avatar
    Joined
    Mar 2005
    Posts
    836
    Location
    Have you resolved this yet? This works for me...
    Application.OnTime Now + TimeValue("00:00:01"), "'testing4 ""BART""'"
    This must be in Module code...
    Sub Testing4(strTest As String)
        MsgBox strTest
    End Sub
    HTH. Dave

  3. #3
    Microsoft Word MVP 2003-2009 VBAX Guru gmaxey's Avatar
    Joined
    Sep 2005
    Posts
    3,340
    Location
    Dave,

    No I haven't.

    I copied and pasted your code in the ThisDocument Module and then tried again using a Standard Module in Word 2003, 2007, 2010 and 2013 and also in Word 2010 starting with winword /a and in each case nothing happens when I run the code. I'm just completely stumped!

    Sub Test()
    Application.OnTime Now + TimeValue("00:00:01"), "'testing4 ""BART""'"
    End Sub
    Sub testing4(pstr As String)
      MsgBox pstr
    End Sub
    Greg

    Visit my website: http://gregmaxey.com

  4. #4
    VBAX Expert Dave's Avatar
    Joined
    Mar 2005
    Posts
    836
    Location
    My apologies. I forgot what forum I was in. That code worked for XL. I'll give it a go in Word. Dave

  5. #5
    Knowledge Base Approver VBAX Guru macropod's Avatar
    Joined
    Jul 2008
    Posts
    4,435
    Location
    Hi Greg,

    Let's start with the basics - does this work for you:
    Sub Demo()
    MsgBox Now()
    Application.OnTime Now() + TimeValue("00:00:05"), "Demo"
    End Sub
    Cheers
    Paul Edstein
    [Fmr MS MVP - Word]

  6. #6
    Microsoft Word MVP 2003-2009 VBAX Guru gmaxey's Avatar
    Joined
    Sep 2005
    Posts
    3,340
    Location
    Paul. Yes it works.

    So does this:


    Sub Demo()
        MsgBox Now()
        Application.OnTime Now() + 0.00005, "Demo"
    End Sub
    Both work similar to the first three tests in my posted code works.

    Thanks.
    Greg

    Visit my website: http://gregmaxey.com

  7. #7
    Knowledge Base Approver VBAX Guru macropod's Avatar
    Joined
    Jul 2008
    Posts
    4,435
    Location
    Hi Greg,

    I missed the part from your original post re passing arguments. Word's OnTime method doesn't support them. Instead, it has a 'tolerance' parameter to accommodate other timing issues.
    Cheers
    Paul Edstein
    [Fmr MS MVP - Word]

  8. #8
    Microsoft Word MVP 2003-2009 VBAX Guru gmaxey's Avatar
    Joined
    Sep 2005
    Posts
    3,340
    Location
    Paul,

    No problem. At least I know that I was wrong to think I had done it before and can stop trying to pound a square peg in a round hole.

    This will work for my purposes:

    Sub Test1()
      Application.OnTime When:=Now + 0.000005, Name:="modMain.Test"
    End Sub
    Sub Test()
      TestWithParameters "Test 1 string passed"
    End Sub
    Sub TestWithParameters(ByRef strPassed As String)
      MsgBox strPassed
    End Sub
    Last edited by gmaxey; 10-04-2013 at 02:10 PM.
    Greg

    Visit my website: http://gregmaxey.com

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •