Share via

Macro on keypress

Anonymous
2014-06-16T04:37:21+00:00

Sub thes()

SendKeys ("+{F7}")

SendKeys ("{RIGHT}")

End Sub

This code was the simplest way for me to open my thesaurus in my word doc without leaving the current word selected.

shift/F7 opens the thesaurus window and the right arrow key then unselects the word allowing me to keep typing in the doc.

If I could just get this macro to fire every time I pressed a key it would be wonderful.  This would open a dynamically changing thesaurus window on the right side of my screen without interrupting my typing in the doc.

Any hints on accomplishing this "on keypress" event as well as any code refining suggestions would be most appreciated.

Thank you.

Microsoft 365 and Office | Word | For home | Windows

Locked Question. This question was migrated from the Microsoft Support Community. You can vote on whether it's helpful, but you can't add comments or replies or follow the question.

0 comments No comments

Answer accepted by question author

Anonymous
2014-06-17T12:05:03+00:00

Maybe you could use the WindowSelectionChange Application event...

See http://word.mvps.org/faqs/macrosvba/appclassevents.htm for instructions on how to set up Application Events in Word.  You would then be able to type normally, but as soon as you click on a word or move the cursor with the arrow keys over a word the Thes procedure would run.  Follow the instructions in the above link.  Then:

in the normal module:

Option Explicit

Dim oAppClass As New ThisApplication

Public Sub AutoExec()

     Set oAppClass.oApp = Word.Application

End Sub

Sub Thes()

    Dim rng As Range

    Set rng = Selection.Range

    Application.CommandBars.ExecuteMso ("Thesaurus")

    rng.Select

End Sub

in the ThisApplication class module:

Option Explicit

Public WithEvents oApp As Word.Application

Private Sub oApp_WindowSelectionChange(ByVal Sel As Selection)

    Call Thes

End Sub

Cheers

Rich

PS. And you could modify the oApp_WindowSelectionChange procedure so it only calls Thes when the Thesaurus has been manually opened by you (to allow you to close it and it will stay closed) like this:

Private Sub oApp_WindowSelectionChange(ByVal Sel As Selection)

    If Application.CommandBars.GetPressedMso("ResearchPane") Then Call Thes

End Sub

Was this answer helpful?

0 comments No comments

7 additional answers

Sort by: Most helpful
  1. Anonymous
    2014-06-18T01:06:40+00:00

    You know what might also be neat to try is a timer of some sort.  I notices that there is no timer object on the toolbar when I pull up a form.  That's too bad.  I could run the thesaurus every half second or so.  I'll do some research to see if I can find a makeshift programmatic timer or something that might work too.

    Thanks,

    Steve

    Was this answer helpful?

    0 comments No comments
  2. Anonymous
    2014-06-18T00:42:59+00:00

    Alright, that was tough.  I've never delved that deeply into the process of writing macros.  I proceeded humbly and with no confidence whatsoever, yet I succeeded completely.

    It worked beautifully, just as you suggested.

    It will be very helpful, though I must say...

    It is bittersweet.  If only I could find code to make those synonyms appear without backing into the word or clicking on it, like it does when you press alt-F7.  Do you know what I mean?  If your cursor is just to the right of the word and you activate the thesaurus Word recognizes that you want synonyms for the word you just typed.

    Nonetheless, I have grown much in one day and seen that my baby has gotten her bath.  At least for the former, you have my deepest gratitude.

    Thanks again.

    Steve

    Was this answer helpful?

    0 comments No comments
  3. Anonymous
    2014-06-17T23:15:21+00:00

    I like the second option, the windowsselectionchange option that is.  I plugged in the first suggestion and it worked perfectly.  Thank you for that.  When you asked what key I had in mind- it was actually all of the keys I was hoping for.  I was wanting Word to give me an array of synonyms for every word I typed.  I'm beginning to think I'm asking too much of the program.

    The second suggestion looks like a happy medium.  I know I should have tried it before replying, but I wanted to offer my thanks as soon as I saw your suggestions.  I have a one year old that needs a bath.  As soon as she's asleep for the night I'll get on putting together the other suggestion.  I looks like it will be very helpful.

    As an amateur writer and poet, a dynamic thesaurus operating out of the corner of my eye with little or no prodding from me, seems like a wonderful think.  Hopefully, it won't be just a flight of fancy.  If it is, I'll just write about one instead.

    Thanks,

    Steve

    Was this answer helpful?

    0 comments No comments
  4. Anonymous
    2014-06-17T11:57:29+00:00

    Hi,

    Try this:

    Sub Thes()

        Dim rng As Range

        Set rng = Selection.Range

        Application.CommandBars.ExecuteMso ("Thesaurus")

        rng.Select

    End Sub

    Sub AssignKeyBindings()

        CustomizationContext = NormalTemplate

        Application.KeyBindings.Add _

            KeyCode:=BuildKeyCode(wdKeyT), _

            KeyCategory:=wdKeyCategoryCommand, _

            Command:="Thes"

    End Sub

    Sub RemoveKeyBindings()

        Dim kb As KeyBinding

        CustomizationContext = NormalTemplate

        For Each kb In Application.KeyBindings

            If kb.KeyCode = BuildKeyCode(wdKeyT) Then kb.Clear

        Next kb

    End Sub

    Run the AssignKeyBindings macro to make the thesaurus run each time you press the t button.

    Run the RemoveKeyBindings to revert to the normal behaviour.

    You didn't say which key you wanted this macro to run on...  I guessed at "t", but you could change it to a combination of Ctrl and/or Alt.  there is no "on key press" event in Word as far as I know.

    Hope that helps.

    Cheers

    Rich

    Was this answer helpful?

    0 comments No comments