Ah, I see. You are correct. IF you make a shortcut key through either of those routes what happens is this:
Word
executes code similar to
Code:
CustomizationContext = NormalTemplate
KeyBindings.Add KeyCode:=BuildKeyCode(wdKey3, wdKeyAlt), _
KeyCategory:= _
wdKeyCategoryStyle, Command:="Blah"
This makes a keybinding (in this case Alt-3), which fires a command in the Style category of "Blah". That is, it makes the current paragraph the Style "Blah"
HOWEVER, this is executed by Word, and
makes the keybinding. It does NOT make a Sub. The keybinding is stored in whatever you chose to store it in. The default is Normal.dot, but you can put it in the current active document if you choose.
Again, the keybinding is executed; there is NO Sub created. In which case, there is no Sub to edit and make into a toggle. To repeat, when you make a shortcut via Tools > Customize, or Format > Style > Shortcut key, there is NO Sub to edit. You are making a one-time, one-way,
keybinding.
If this is the case, all my stuff written above is beside the point. I rarely do shortcuts by those routes BECAUSE I want to be able to edit what the shortcut executes (if I want/need to).
bartoshw, if you indeed made your shortcut keys via Customize, or any other menu based process, then...you can not edit them, you can not
change them into a toggling.
HOWEVER, you can do this. Record a new
blank macro and use the
same keyboard shortcut. A dialog will display showing that that shortcut is already being used. Assign it anyway.
This executes a
new one-time keybinding, making it apply to the Sub created by the recorded macro.
Code:
Sub ChangeMyAlt_J()
'
' ChangeMyAlt_J Macro
' recording a blank macro in order
' to change shortcut key
'
End Sub
In the example above, there was a shortcut (Alt-j) that was attached to a Style. This was set using the menus (Tools > Customize > Style...etc.)
By recording a
blank macro that overwrites the shortcut, I can now put whatever I want into the Alt-j shortcut.
This can include the original instruction - make current paragraph Style "whatever" - and ALSO additional logic...such as toggling.
So say you had originally set a shortcut (Alt-j) as Style "Yadda" via menus (Tools > etc, OR Format > Style etc.) Again, this means Word
executed:
Code:
[code]
CustomizationContext = NormalTemplate
KeyBindings.Add KeyCode:=BuildKeyCode(wdKeyJ, wdKeyAlt), _
KeyCategory:= _
wdKeyCategoryStyle, Command:="Yadda"
NO Sub was created.
By recording a blank macro, you can now put in the original intention - make it Style "Yadda" - and ALSO have additional logic. That logic can be
anything.
Code:
Sub ChangeMyAlt_J()
' shortcut = Alt-j
' these are the NEW instructions for Alt-j
If InStr(1, Selection.Paragraphs(1).Range.Text, "Gerry") _
> 0 Then
Selection.Paragraphs(1).Style = "Yadda"
Else
Selection.Paragraphs(1).Style = "Normal"
End If
End Sub
The above simply shows that you can - now that you are able to edit the instructions attached to the shortcut - do whatever you want. In this case, the code checks to see if the paragraph the selection is in contains the word "Gerry". If it does, it makes the paragraph style "Yadda". If the paragraph does not contain "Gerry", it makes the paragraph style "Normal".
This sentence has Gerry in this. (becomes style "Yadda")
This sentence does not have that word. (becomes "Normal")
Lastly, the name you give the macro is not (unless you want it to be) not really relevant, as you are going to use a keyboard shortcut anyway. However, it WILL (and must) have a name.
I would also like to emphasize that one must be very careful in the dialogs that you are storing the macro (or shortcuts for that matter) in the place you want to store them. Remember, Word stores things in Normal.dot by default.
Normal.dot is not a good place to store things. I have virtually nothing in Normal.dot. I store all my macros, shortcuts, etc. in a global template designed to do precisely, and only, that - store macros and shortcuts.
faq219-2884
Gerry
My paintings and sculpture