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

    Can I trap the Enter button pressed in a document? (Word 2003)

    Hi all,

    RE: Can I trap the Enter button pressed in a document?

    I am in a document , and when I want to run a certain macro that I wrote, I presss ALT+Enter (as the next best thing to Enter).

    But now I am wondering if it is possible to trap the Enter button (remember: I am in a document, and not in a form)

    [and then the macro I wrote would test the paragraph's content and do what I need to do depending on what I find in the paragraph,
    otherwise pass control on to Word to do its usual stuff].

    Tia,

    avi

  2. #2
    Plutonium Lounger
    Join Date
    Mar 2002
    Posts
    84,353
    Thanks
    0
    Thanked 29 Times in 29 Posts

    Re: Can I trap the Enter button pressed in a document? (Word 2003)

    Yes, like this:

    Sub TrapEnter()
    ' Trap the Enter key
    Application.CustomizationContext = ActiveDocument
    Application.KeyBindings.Add wdKeyCategoryMacro, "MyMacro", wdKeyReturn
    End Sub

    Sub UnTrapEnter()
    ' Release the Enter key
    Application.CustomizationContext = ActiveDocument
    Application.FindKey(wdKeyReturn).Clear
    End Sub

    Sub MyMacro()
    ' Your action for the Enter key goes here
    Beep
    End Sub

  3. #3
    2 Star Lounger
    Join Date
    Dec 2007
    Posts
    179
    Thanks
    3
    Thanked 0 Times in 0 Posts

    Re: Can I trap the Enter button pressed in a document? (Word 2003)

    Great!
    I mean, So far so good.

    And then how would I invoke within MyMacro() the default Word processing for Enter?
    Like this:

    Sub MyMacro()
    ' Your action for the Enter key goes here
    Beep
    Word.Enter 'something like this- to invoke the default Word processing for Enter
    End Sub

    Tia,

    - avi

  4. #4
    Plutonium Lounger
    Join Date
    Mar 2002
    Posts
    84,353
    Thanks
    0
    Thanked 29 Times in 29 Posts

    Re: Can I trap the Enter button pressed in a document? (Word 2003)

    You can use

    Selection.TypeParagraph

    to simulate the action of the Enter key.

  5. #5
    2 Star Lounger
    Join Date
    Dec 2007
    Posts
    179
    Thanks
    3
    Thanked 0 Times in 0 Posts

    Re: Can I trap the Enter button pressed in a document? (Word 2003)

    That doesn't cause a recursive invocation of the MyMacro subroutine, so causing an infinite loop?

  6. #6
    Plutonium Lounger
    Join Date
    Mar 2002
    Posts
    84,353
    Thanks
    0
    Thanked 29 Times in 29 Posts

    Re: Can I trap the Enter button pressed in a document? (Word 2003)

    No, it doesn't because you're not really simulating the Enter key but executing its default action (inserting a paragraph mark).

  7. #7
    2 Star Lounger
    Join Date
    Dec 2007
    Posts
    179
    Thanks
    3
    Thanked 0 Times in 0 Posts

    Re: Can I trap the Enter button pressed in a document? (Word 2003)

    Thanks!

    avi

Posting Permissions

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