Microsoft Office Forums

Go Back   Microsoft Office Forums > >

Reply
 
Thread Tools Display Modes
  #1  
Old 06-25-2020, 01:43 AM
alex100 alex100 is offline Assigning shortcut keys via a macro file Windows 7 64bit Assigning shortcut keys via a macro file Office 2016
Advanced Beginner
Assigning shortcut keys via a macro file
 
Join Date: May 2020
Posts: 79
alex100 is on a distinguished road
Default 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
source: VBA Word: how to call a function via shortcut key?

As an example, what I need is to call "MyScript1" subroutine when pressing Ctrl + L, and "MyScript2" subroutine when pressing Alt + L.

Alex
Reply With Quote
  #2  
Old 06-25-2020, 04:10 PM
Guessed's Avatar
Guessed Guessed is offline Assigning shortcut keys via a macro file Windows 10 Assigning shortcut keys via a macro file Office 2016
Expert
 
Join Date: Mar 2010
Location: Canberra/Melbourne Australia
Posts: 3,932
Guessed has a brilliant futureGuessed has a brilliant futureGuessed has a brilliant futureGuessed has a brilliant futureGuessed has a brilliant futureGuessed has a brilliant futureGuessed has a brilliant futureGuessed has a brilliant futureGuessed has a brilliant futureGuessed has a brilliant futureGuessed has a brilliant future
Default

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
Reply With Quote
  #3  
Old 06-26-2020, 01:28 AM
alex100 alex100 is offline Assigning shortcut keys via a macro file Windows 7 64bit Assigning shortcut keys via a macro file Office 2016
Advanced Beginner
Assigning shortcut keys via a macro file
 
Join Date: May 2020
Posts: 79
alex100 is on a distinguished road
Default

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
Alex
Reply With Quote
  #4  
Old 06-26-2020, 02:18 AM
Guessed's Avatar
Guessed Guessed is offline Assigning shortcut keys via a macro file Windows 10 Assigning shortcut keys via a macro file Office 2016
Expert
 
Join Date: Mar 2010
Location: Canberra/Melbourne Australia
Posts: 3,932
Guessed has a brilliant futureGuessed has a brilliant futureGuessed has a brilliant futureGuessed has a brilliant futureGuessed has a brilliant futureGuessed has a brilliant futureGuessed has a brilliant futureGuessed has a brilliant futureGuessed has a brilliant futureGuessed has a brilliant futureGuessed has a brilliant future
Default

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.
Reply With Quote
  #5  
Old 06-26-2020, 05:35 AM
eduzs eduzs is offline Assigning shortcut keys via a macro file Windows 10 Assigning shortcut keys via a macro file Office 2019
Expert
 
Join Date: May 2017
Posts: 260
eduzs is on a distinguished road
Default

( sorry )

Last edited by eduzs; 06-26-2020 at 09:05 AM.
Reply With Quote
  #6  
Old 06-26-2020, 06:59 AM
alex100 alex100 is offline Assigning shortcut keys via a macro file Windows 7 64bit Assigning shortcut keys via a macro file Office 2016
Advanced Beginner
Assigning shortcut keys via a macro file
 
Join Date: May 2020
Posts: 79
alex100 is on a distinguished road
Default

Quote:
You don't need to keep running this code every time a doc is opened.
Oh, ok, so it's enough to run the code once for the shortcuts to be saved.

All right, thank you for the clarification!

Alex
Reply With Quote
  #7  
Old 06-26-2020, 09:27 AM
eduzs eduzs is offline Assigning shortcut keys via a macro file Windows 10 Assigning shortcut keys via a macro file Office 2019
Expert
 
Join Date: May 2017
Posts: 260
eduzs is on a distinguished road
Default

( sorry, if needed I will post a new topic )

Last edited by eduzs; 06-26-2020 at 04:02 PM.
Reply With Quote
Reply

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
Assigning shortcut keys via a macro file 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

Other Forums: Access Forums

All times are GMT -7. The time now is 04:43 AM.


Powered by vBulletin® Version 3.8.11
Copyright ©2000 - 2024, vBulletin Solutions Inc.
Search Engine Optimisation provided by DragonByte SEO (Lite) - vBulletin Mods & Addons Copyright © 2024 DragonByte Technologies Ltd.
MSOfficeForums.com is not affiliated with Microsoft