#1
|
|||
|
|||
Assigning shortcut keys via a macro file
How do I assign a shortcut key to a MS Word subroutine?
I already know how to do it via the "Customize Ribbon" Options menu, but I'm interested in hardcoding the keys inside a macro file. I understand I need to use something like the code below, but where exactly do I insert this code? I tried adding it to the 'ThisDocument' section, without any success. Code:
With Application .CustomizationContext = ThisDocument .KeyBindings.Add KeyCode:=BuildKeyCode(wdKeyF1), _ KeyCategory:=wdKeyCategoryCommand, _ Command:="printAllRecords" End With As an example, what I need is to call "MyScript1" subroutine when pressing Ctrl + L, and "MyScript2" subroutine when pressing Alt + L. Alex |
#2
|
||||
|
||||
ThisDocument is the file the code you are running sits in.
If you wanted the keyboard shortcut to work on all documents you could put it in .CustomizationContext = NormalTemplate or restrict it to a specific template .CustomizationContext = ActiveDocument.AttachedTemplate I recommend you set the keyboard context to the same template that holds the code it is trying to run. You might also include the module name to ensure there is no confusion in case you have the same macro name in more than one module. Command:="ModuleName.printAllRecords"
__________________
Andrew Lockton Chrysalis Design, Melbourne Australia |
#3
|
|||
|
|||
Thank you for your answer, Andrew!
I also found some helpful, step-by-step information in the following article: VBA Express : Word - Add Keybinding to document to launch Macro Anyhow, what I'm now using is the code below, and it works good! I inserted it in the 'ThisDocument' file, and it gets executed automatically when the document is opened (this was actually the main thing I was missing!). Code:
Private Sub Document_Open() With Application .CustomizationContext = ThisDocument .KeyBindings.Add KeyCode:=BuildKeyCode(wdKeyControl, wdKeyL), KeyCategory:=wdKeyCategoryCommand, Command:="MyScript1" .KeyBindings.Add KeyCode:=BuildKeyCode(wdKeyAlt, wdKeyL), KeyCategory:=wdKeyCategoryCommand, Command:="MyScript2" End With End Sub |
#4
|
||||
|
||||
Alex,
You don't need to keep running this code every time a doc is opened. I'm assuming you put the code in your normal template so it only needed to be run once. You can rename the macro now as it is overkill to update your template (and be asked to save it) every time you open a doc. |
#5
|
|||
|
|||
( sorry )
Last edited by eduzs; 06-26-2020 at 09:05 AM. |
#6
|
|||
|
|||
Quote:
All right, thank you for the clarification! Alex |
#7
|
|||
|
|||
( sorry, if needed I will post a new topic )
Last edited by eduzs; 06-26-2020 at 04:02 PM. |
Thread Tools | |
Display Modes | |
|
Similar Threads | ||||
Thread | Thread Starter | Forum | Replies | Last Post |
macro used to activate shortcut keys | hudsontucker13 | PowerPoint | 1 | 12-14-2013 09:47 AM |
Word VBA: assign shortcut keys to sub??? | tinfanide | Word VBA | 2 | 12-05-2011 03:41 AM |
Outlook Mail Shortcut Keys? | gratiot | Outlook | 3 | 07-29-2011 10:31 AM |
cant use shortcut keys (ctrl+c/v/z...) | laro | Office | 0 | 10-08-2010 11:01 PM |
Can't Assign Shortcut Keys | Vicki F. | Word | 0 | 04-17-2010 03:03 PM |