Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations gkittelson on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

How do I disable a certain key or macro in VBA for Word? 1

Status
Not open for further replies.

nesplb

Programmer
Jul 29, 2003
109
NO
Hi!
How do I disable a certain hotkey for example "ctrl + 1" in Word?

I want to disable a macro, either by disable it's shortcut or disable the entire macro from the code in another macro.

Does anyone know how?
 
I don't really understand what you mean. Are you trying to stop users from starting a macro that you have in another Doc or in the same doc? What is the macro for? Why do you what to disable it anyway?

I need specifics to try to give you the best option possible.



If you can't be "The Best", be the best at what you can!!!

Never say Never!!!
Nothing is impossible!!!
 
Ok. I'll try to explain:

I have a macro, macro1, who starts when a user clicks "ctrl + 1". And I have another macro, macro4, who starts when a user clicks "ctrl + 4". If the user press "ctrl +1" I want macro1 to prevent the user from starting macro4 if he press "ctrl +4".

So I guess I can do this in two ways:

1: Write some code in macro1 that disables macro4.

2: Write some code in macro1 that disables the combination of the keys "ctrl + 4"

The macros insert a special heading for a word template.
So heading1 = "ctrl +1", heading2 = "ctrl +2", heading3 = "ctrl+3" heading4 = "ctrl+4" etc.
The goal of this project is to prevent a user inserting for example heading4 right after heading1.

I found some code for option 2: but it still don't work with the numeric keys. It's supposed to disable "ctrl+4"

code:
FindKey(KeyCode:=BuildKeyCode(wdKeyControl, 52)).disable

please help!!!

 
Hi nesplb,

Specifically, to disable (or do anything else to) the 4 key you need to use wdKey4 (value 52) as you have done. To disable (or do anything else to) the Nemeric Keypad 4 key you need to use wdKeyNumeric4 (value 100). More generally, see Thread68-599869 for how to disable/enable keys in both Word and Excel.

Disabling the key combination(s), of course, does not disable the macro. If you want to do that you will have to code it yourslef via global variables or similar.

Enjoy,
Tony
 
Thank you very much for the helpful tip, but I still have one problem...
I can now disable "ctrl +4" with this code:

KeyBindings.Add KeyCode:=BuildKeyCode(wdKeyControl, 52), KeyCategory:=wdKeyCategoryDisable, Command:=""

When I try to enable it again it doesn't work with this code:

KeyBindings.Key(BuildKeyCode(wdKeyControl, 52)).Clear

I get the error message: "Run time error: 91, Object variable or with block not set"
Do you know what's wrong?
 
Sorry for bothering you...I figured it out!:)

I could enable the "ctrl+4" simply by adding this code:

KeyBindings.Add KeyCode:=BuildKeyCode(wdKeyControl, 52), KeyCategory:=wdKeyCategoryMacro, Command:="Normal.NewMacros.macro4"

-Now I won't lose my job....Thank you very much!:)
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top