Results 1 to 5 of 5
  1. #1
    2 Star Lounger
    Join Date
    Dec 2007
    Posts
    171
    Thanks
    3
    Thanked 0 Times in 0 Posts

    Word VBA: How to make a VBA program wait for a key press on a document

    Hi All,

    How do you make a Word VBA program wait for a key press while you have a document window active?

    I.e., you are working in document -- not a windows form -- and you have started up a macro that is now running, and you want to the macro to pause until you press some key on the keyboard, e.g., the Enter key or the space bar key. Once you have pressed the key, the macro continues to run.

    TIA

    -avraham

    c
    Last edited by amakeler; 2013-10-22 at 19:21.

  2. Subscribe to our Windows Secrets Newsletter - It's Free!

    Get our unique weekly Newsletter with tips and techniques, how to's and critical updates on Windows 7, Windows 8, Windows XP, Firefox, Internet Explorer, Google, etc. Join our 480,000 subscribers!

    Excel 2013: The Missing Manual

    + Get this BONUS — free!

    Get the most of Excel! Learn about new features, basics of creating a new spreadsheet and using the infamous Ribbon in the first chapter of Excel 2013: The Missing Manual - Subscribe and download Chapter 1 for free!

  3. #2
    Super Moderator RetiredGeek's Avatar
    Join Date
    Mar 2004
    Location
    Manning, South Carolina
    Posts
    5,813
    Thanks
    185
    Thanked 703 Times in 641 Posts
    Avraham,

    Knowing why you want to pause the code would be most helpful in providing a good answer to your question.
    May the Forces of good computing be with you!

    RG

    VBA Rules!

    My Systems: Desktop Specs
    Laptop Specs


  4. #3
    2 Star Lounger
    Join Date
    Dec 2007
    Posts
    171
    Thanks
    3
    Thanked 0 Times in 0 Posts
    Hi Mr. Geek,

    Thanks for getting back to me.

    >> Knowing why you want to pause the code would be most helpful in providing a good answer to your question.

    Well I could want to know just for general knowledge... :-)

    Ok - so the reason I want to do this is as follows.

    I have written a type of command interpreter in Word VBA to execute a type of script held in a small Word document. The script comprises a list of simple commands. I run the script my clicking a command button on the Ribbon.

    What is important is that my VBA command interpreter has a loop that reads and executes the commands one by one. Once I run the command interpreter, it will "zip" through and execute all the commands in the script very quickly and without stopping. I would like to slow down the execution of the script by adding a 'wait' mechanism that, after the execution of each command, waits for the user to press a keyboard key, and then the interpreter will execute one more command, and so on.

    Does that help?

    TIA

    avraham

  5. #4
    Super Moderator RetiredGeek's Avatar
    Join Date
    Mar 2004
    Location
    Manning, South Carolina
    Posts
    5,813
    Thanks
    185
    Thanked 703 Times in 641 Posts
    Avraham,

    One thing you could try would be to have the execution loop call a VERY Small form with a single OK button and assign a key to the button that would execute closing of the form and return to the calling code. Attached is a sample file which contains one Module & one Userform. HTH
    TestLoopPauseButton.xlsm
    May the Forces of good computing be with you!

    RG

    VBA Rules!

    My Systems: Desktop Specs
    Laptop Specs


  6. #5
    2 Star Lounger
    Join Date
    Dec 2007
    Posts
    171
    Thanks
    3
    Thanked 0 Times in 0 Posts
    Thanks.
    Well I could do that by displaying a message box also, I suppose.

    I just tried to do it this way - as below - where I create a loop that stops only when the loop control changes state.
    I tried to then invoke a macro with a key click (yes -while the first macro is still running, but that did not work. Obviously VBA does not allow that...

    Public Sub ScheduleContinue()
    g_bScheduleContinue = True
    End Sub

    For Each rowCmd In tblCmdInp.Rows

    g_bScheduleContinue = False
    MsgBox ("Getting ready to wait...")

    Do
    <process>
    DoEvents
    Loop Until g_bScheduleContinue = True

    MsgBox ("After wait...")

    Next rowCmd

Posting Permissions

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