How do I create and use a keyboard hook to trigger VBA macros?

This is related to the questions "How do I trigger a macro when someone types" or "Is there a typing event in VBA for MS Word". The answer has generally been you need to create a winAPI keyboard hook and use it to trigger your macro - but none of the answers I could find actually told us HOW to create the hook and none of the example code I could find would work. Most of the google results I could find on this topic lead to 404's or moved articles.

Edit: The end goal is to be able to fire an event any time the document body changes, so run a function any time a single letter key is pressed, and to use the number keys 0 to 9 as hotkeys without using Ctrl, Alt, or any modifier keys.

Could someone who has a better grasp of VBA keyboard hooks please post an example of a macro function that is triggered by any keypress? Or by pressing particular keys?

  • by keyboard "hook" do you mean a keyboard shortcut key? – Prasanna Sep 9 '15 at 15:08
  • record a macro with a shortcut, then put your macro into that – Raystafarian Sep 9 '15 at 15:10
  • The regular way of using keyboard shortcuts won't work for my application, eg using Alt+1, or Ctrl+enter and assigning them to the macro. I need to run a macro every time the document text changes eg every time a letter or number key is pressed, which requires setting "a", "b", "c", etc as hotkeys. Not only would this be very messy but the normal setting hotkeys to macros method doesn't work. I'm talking about a winAPI keyboard hook, through system32.dll. I've found it mentioned in several similar questions but yet to find any decent example that I could get working. – user6212 Sep 9 '15 at 15:33
  • *By "setting normal hotkeys doesn't work" I meant it doesn't work for single keys, it will only let you set key-combos with alt, ctrl, shift, etc. Worksheet_Change(ByVal Target As Range) looks perfect if I was using excel, but I need this for microsoft word. – user6212 Sep 9 '15 at 16:04
  • There is no "document change" event for word. Just FYI – Raystafarian Sep 10 '15 at 15:20

This was done on 2010, but should be similar for 2007.

You can create your macro and then go to options - customize - keyboard shortcuts: customize

enter image description here

And assign it through there.

enter image description here

Alternatively, since this can't be accomplished with VBA, you need to record a macro assigned to a shortcut, then change the code.

In the developer tab hit "record macro" and assign macro to keyboard -

enter image description here

Then, assign your shortcut -

enter image description here

When you stop recording, you can go into VBE and change it to what you need. There will be no indication in the code that it has a keyboard shortcut.

  • This solution is not viable for my use, I need to assign 2 macros: One macro will fire when any letter is pressed (single letter, NOT Alt+letter or trl+letter) and one when enter or any number key is pressed (once again, single key hotkey) The intended effect is to run one macro (function) every time the text in the document changes, eg when the user types, and a second macro when any number or enter key is pressed. – user6212 Sep 18 '15 at 17:31

Short Answer: Don't try to do this in VBA. Find another means of mapping 0-9 to shortcut keys that Word understands. Microsoft IntelliType drivers can do this and so can AutoHotKey. Both have the ability to do this mapping for just Word, without affecting other applications. AutoHotKey has much more power, and can easily map a single key into many keystrokes.

Long Answer: Keyboard hooks are very difficult to get right even in languages like VB.NET and and C#, since they are not part of .NET. For example, if you hook the keyboard queue, but fail to unhook when done very bad things (like crashing Visual Studio) happen. C++ is no better, but C++ programmers are used to working in an unforgiving environment. (IWO everything crashes all the time.)

I recommend looking at AutoHotKey. On the upside, keyboard macros are its strength. The downside is that the syntax is inconsistent. I have to look at my cheat sheet after many years of intermittently using it.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.