View Full Version : assign Hot key from with in VBA
cjmitton
11-20-2012, 03:37 AM
I need to assign a macro a hot key of ALT M to run my memo opening macro, I know how to do it with-in word 2010 its self but dont fancy running around 45 PC doing this!
Is there a Macro I can write to assign this hotkey? then put it to the macro I deploy via GPO to my users...
I recorded the following but sadly it did not pick up the macro I was assigning (guessing it goes in the command:="" part). It also looks like it is a run once type of command where I really want to assign this so it can be used for another command.
CustomizationContext = NormalTemplate
KeyBindings.Add KeyCode:=BuildKeyCode(wdKeyM, wdKeyAlt), KeyCategory:= _
wdKeyCategoryCommand, Command:=""
Thanks
gmaxey
11-20-2012, 07:10 AM
Something like:
Sub AutoNew()
SetKeyBinding
End Sub
Sub AutoOpen()
SetKeyBinding
End Sub
Sub AutoClose()
ClearKeyBinding
End Sub
Sub YourMacro()
MsgBox "I'm running."
End Sub
Sub SetKeyBinding()
CustomizationContext = ActiveDocument.AttachedTemplate
KeyBindings.Add _
KeyCode:=BuildKeyCode(wdKeyM, wdKeyAlt), _
KeyCategory:=wdKeyCategoryCommand, _
Command:="YourMacro"
End Sub
Sub ClearKeyBinding()
CustomizationContext = ActiveDocument.AttachedTemplate
Dim kbLoop As KeyBinding
For Each kbLoop In KeyBindings
kbLoop.Clear
Next kbLoop
End Sub
cjmitton
11-20-2012, 07:18 AM
Thanks, I'll try that one. Just as a quick thought I'll be adding the templates to the 'startup' folder. I trust the auto open / close will work?
cjmitton
12-06-2012, 03:06 AM
Thanks for the code, I got it working... my apologies for the late reply!
Frosty
12-11-2012, 10:32 AM
If you're putting the keybinding in a global addin, you don't need to rebind the key every time the global addin is loaded (which would be AutoExec).
You just need to do that once.
However, I think you're going to run into occasional problems with Word 2007/2010 with just ALT+M, since you're *kind of* confusing two concepts: Keyboard Shortcuts (keybindings) and Accelerators (ALT followed by some key combo).
In word 2010, if you press ALT, you'll see a bunch of values pop up over your ribbon. "ALT, M" takes you to the Mailings tab while "ALT+M" will run your macro. Pressing ALT and then pressing M is different than pressing them together. ALT + any key can be a little confusing, as it can be subtle difference for end-users, depending on the user.
Also, I don't think you need to loop through all keybindings and clear each one.
Application.KeyBindings.ClearAll does that, in whatever customization context you're in.
But basically, the process for programmatically assigning and deploying keybindings in a global template is this:
1. Open your global template
2. With the global template open, run Greg's SetKeyBinding code
3. Save the global template
4. Copy that global template to the Word Startup folder to your users.
Couple of extra notes:
A. I tend to use
CustomizationContext = ThisDocument.AttachedTemplate
instead of
CustomizationContext = ActiveDocument.AttachedTemplate
This keeps me from accidentally not having my global template as the activedocument, and ensures that I'm always assigning keybindings to the same template which contains the macro
B. AutoExec/AutoExit vs AutoOpen/AutoClose/AutoNew
Understanding when these fire is going to be pretty critical about whether you're establishing a default for your end-users or whether you are enforcing a keybinding. I don't *think* you want to assign a keybinding of ALT+M to the attached template of every document you open (which is what Greg's code would do). Otherwise, when you open a document which isn't based on Normal.dotm, you're going to start getting messages about "you've made changes to Letter.dotm/Memo.dotm/etc etc" when you save new documents and then close them. You'll skip that message if you have the "Prompt to save Normal" option unchecked, but it will cause you issues on all of your other templates.
cjmitton
12-12-2012, 03:42 AM
Frosty,
Thanks for your reply. In the end I manually added the relevent ALT-M etc, as I need to do a manual job on each computer but I'm looking to add this to my next revision of company macro's.
Thanks for the notes on the various elements to take in to account, I'm still trying to persude the Secretaries to move to a different keys but considering they've been doing it for 7 years plus its a tough one! so I'll just have to take a hit on using the ALT key.
I do however have a question(s) which take a little explaining.
I currently have individule templates (.dotm) on general company wide macro's like the memo, one for the department specific macro's like letterheads and then one for the printers they print to. Each template would need to assign a different set of ALT+<letter> key binding depending on the relevent departments requirements.
For example:
ALT+M would be in the company wide macros for opening the memo template.
ALT+L would be in the department macros for opening their Letterheaded template.
ALT+P would be in the printer macros to run a particulare print macro.
I need to make sure each of these (plus others as theres a few more) get assigned to the local normal template so that they can be controlled from there. Also I won't want to un-assign them as they should remain in the normal template. would 're-assigning' them each time word opens (and they've not been un-assigned) cause issues?
Frosty
12-12-2012, 07:16 AM
I wouldn't recommend storing the key bindings in normal. Don't you have a global template? Why can't you store the key bindings there?
The problem with storing in normal is that you preclude your end users from using their own additional key bindings if you're wiping then all out each time.
I guess I need to understand your structure a bit more.
Do you have a global template?
You need different sets of key bindings for different departments? Or you want to change a key binding to run a different macro depending on what template is open?
Because what you're describing seems unnecessarily convoluted.
You don't need to open a new letter and assign a bunch of key bindings to the normal template in order to achieve the effect you seem to want (pressing a key combo producing a different effect at different times)
Do you have a global addin which is distributed to everyone and resides in the word startup folder?
cjmitton
12-12-2012, 08:43 AM
I have various templates (.dotm) that are copied via GPO to the users startup folder on the PC.
These templates are the ones I refered to in my previous posting,
i.e. the company wide templates that have the macro's that open the memo's etc.
Department templates for the specific macro's that open things like the letter templates plus some custom forms etc.
then the printer templates to run the printer macro's to print to various trays etc.
Each template has its own custon ribbon created so that my users cannot mess them up!
I think that you've answered my question anyway as it will only affect my templates in the startup folder not the 'normal' template I was getting confused thinking that when my template was opened when word starts that the constant add / remove of the keybindings would affect but the template in start up and the normal template.
I'm off for a short 'anti-computer' break so will pick up on this next tuesday when I can truely focus on it once more, my brain is completely fried at the moment!
Thanks for your help.
Frosty
12-12-2012, 09:22 AM
Are you sure those templates are all copied to the startup path and not the workgrouptemplates path?
It doesn't make much sense to have Memo.dotm in word's startup folder.
Do these template specific ribbon tabs show up all the time? Or only when a document based on that template is active on the screen?
I think you're missing some basic concepts here. I can't really tell you the "right" way to implement, since there are many right ways. But you asking these questions suggests to me that you would benefit from understanding the .CustomizationContext property better, as well as the difference between a AttachedTemplate and a global addin (in the templates and addins dialog, the global addins are at the bottom of the dialog while the attached template is at the top)
Frosty
12-12-2012, 09:27 AM
Just a quick follow up... Key bindings in global addins are always available. Key bindings in attached templates are only available when a document is open which is based on that template (i.e., attached to that template).
So if you have ALT + M in a global addin (or normal) assigned to mymacro1... And then you have ALT + M assigned to mymacro2, but only in memo.dotm which is only "available" to memo documents.. When you're not in a memo-based document, you'll run mymacro1.
This is somewhat related to the programming concepts of scope and visibility.
Hope this helps, when you come back.
cjmitton
12-12-2012, 09:52 AM
I think we're talking at cross purposes here! The key allocation is not in the memo template. That is in the workgroup template area as you correctly identified. I have separate macro enabled templates which are copied to the local start up folder. These templates have the various macros to call up things like the memo from the work group location by using a simple key press in the circumstance the ALT + M combination.
I want the key presses allocated to the templates with the macros (in the start up folder) not the with in the memo template (in the workgroup location)
I hope this clears up the set up I'm running
Frosty
12-12-2012, 10:18 AM
So you have different global addins being pushed to different people, based on their departmental membership?
I guess what I'm missing here is why you ever needed to run around to 45 machines assigning hotkeys manually.
If you're deploying global-addins, you just open the global-addin, manually assign the keybindings (or use a macro to do it), and then deploy the update to the global-addin.
If there is a problem with the keybindings not working (for example, you've assigned ALT + M to your memo macro in one global addin, but an end-user has assigned ALT + M in their local normal.dotm to Inserting a cross reference), then you could deploy code in your global addin to make sure normal.dotm doesn't contain any conflicting keybindings (which you would run during the AutoExec of your global addin).
I guess-- what's the problem you're trying to solve? Because it seems like the initial "real" answer to your question is: keybindings don't have to exist only in normal.dotm, so therefore, you don't ever need to run around to 45 machines in order to deploy key bindings.
I hope I'm not sounding snippy-- I'm trying to help and understand your structure so I can give you the best advice.
cjmitton
12-12-2012, 10:29 AM
Thanks, what I was missing is that the key bindings will be saved in the template I deployed as a global add in. When I did this previously the key bindings did not follow when I set them. So had to manually add the key bindings so hence where I thought my problem was.
Don't worry how your reply sounded, its always hard to work out set ups especially when I've explained it so poorly. Sadly my terminology on explaining certain setups causes confusion as everything I know is self taught so hence the confusion when I don't use the correct Microsoft terminology!
Thanks for your help as its explained a lot.
Powered by vBulletin® Version 4.2.5 Copyright © 2025 vBulletin Solutions Inc. All rights reserved.