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!

Help! How to turn off a shortcut key after it is used?

Status
Not open for further replies.

bartoshw

Technical User
Sep 23, 2008
3
Hi,
I use MS Word 2007 for Mac. I have created a number of self-made shortcut keys (mostly specifying the font size in subscript and lowered position -- I use Word for writing scientific papers where such devices are needed). Once I use a shortcut key combination I get what I want. But using the same combination of keys does NOT bring me back to the Normal style, and I have to go to the style list and look for the "main text" Normal style manually. This is insane. The on/off key combinations work *only* for default shortcuts (like for italics, bold, underline, subscript and superscript, etc). Self-created shortcuts seem to work only for the application of a created style, not for going back to Normal. Is there a way for making it work both ways?
Thanks!
 
But using the same combination of keys does NOT bring me back to the Normal style, and I have to go to the style list and look for the "main text" Normal style manually. This is insane. "

No...it is not insane at all. It is quite logical and proper.

I have to say that this is a BAD way to use Word. I am not being critical of you personally. It is - unfortunately - the most common way to use Word.

I have created thousands of documents, and not a single one has Normal style. Not one.

You are correct. Assigned shortcuts do whatever they are assigned to do. This can be literally anything. They are NOT, by default, toggles. They can be made into toggles though.

Essentially, the internal built-in shortcuts (at least regarding formatting) do this (for example):

If Bold is ON, turn OFF.
If Bold is OFF, turn ON.

This is what makes them toggle.

In other words your assigned shortcuts do EXACTLY what you have told them to do. Apply whatever it is you have they to do. You have NOT told them them to toggle...so they don't.

"Self-created shortcuts seem to work only for the application of a created style, not for going back to Normal. Is there a way for making it work both ways? "

I will repeat, going "back" to Normal is poor use of Word. If you need character formatting within a Style, then use...Character Styles.

However, is there a way of making it both ways? Yes. Go into the code instructions for your shortcut, and use the appropriate logic.

If THIS, do THAT.
If THAT, do THIS.

faq219-2884

Gerry
My paintings and sculpture
 
Thanks! Make them toggle... Yeah, you must be right. I hope I can find out how to do it.


 
PS.

"However, is there a way of making it both ways? Yes. Go into the code instructions for your shortcut, and use the appropriate logic."

that's the point -- the "appropriate logic" is unclear to me. what is clear is that the standard shortcut keys toggle and there is no obvious way of making self-made shortcuts toggle. at least not in the "Style" section where shortcut keys are defined, as far as I can see.
 
Please clarify. I do not understand what you mean by "at least not in the "Style" section where shortcut keys are defined".

That is not where they are defined.

In any case, let's look at an example. Say you have made a shortcut as Alt-j...or whatever Mac equivalent.

Essentially your action is going to be a Sub, a procedure. It has a name. Go into the VBE (Visual Basic Editor) and edit the procedure, adding whatever logic you need.

I will repeat, the internal shortcuts DO have toggling logic like:

If Bold is ON, Then
turn Bold OFF
Else
turn Bold ON
End If

Thus, a toggle. But it is a toggle achieved by logic. An IF statement is Boolean, i.e. True or False. So the logic is:

If Bold is ON Then
TRUE: action = turn Bold OFF
Else
FALSE (Bold is OFF): action = turn BOLD ON
End If

That is how it "toggles". This is logic, and can be done for anything.

Going back to the example, say your Alt-j makes the Selection Font.Size = 16 (I am using a very simple example):
Code:
Sub yadda()
' shortcut = Alt-j

  Selection.Font.Size = 16
End Sub
In this case, the shortcut - Alt-j - does ONE thing. It makes the Selection font size = 16. If it already = 16, then of course nothing happens. Technically, that is not correct. The instruction still executes, and the size property is set to 16.

You can edit the procedure and add logic, like this:
Code:
Sub yadda()
' shortcut = Alt-j

If Selection.Font.Size <> 16 Then
   Selection.Font.Size = 16
Else
   Selection.Font.Size = 12
End If
End Sub
Now Alt-j will "toggle" the Selection between 16 pts, and 12 pts. If the Selection IS 16, it makes it 12, if it NOT 16 it makes it 16.

So hitting Alt-j will "toggle" the Selection between 16 pts, and 12 pts. Without knowing your exact instructions that you have made a shortcut for, I can not really get into more detail.

Here is another example. Say I have two Styles, "Yadda" and "Blah".

Yadda: Font= Tunga, Size = 10pts, Bold = False
CenteredAligned, SpaceAfter = 12 pts

Blah: Font= Arial, Size = 12pts, Bold = True,
LeftAligned, Indent = Left 1.2 cm
SpaceAfter = 0

The follow "toggles" the styles for the paragraph the Selection (the cursor) is in. Note that the instructions action the whole paragraph. In other words, you do not need to select the paragraph, just put the Selection (cursor) anywhere IN the paragraph.
Code:
Sub myToggle()
'
'  shortcut = Alt-j
If Selection.Style = "Yadda" Then
   Selection.Paragraphs(1).Range.Style = "Blah"
Else
   Selection.Paragraphs(1).Range.Style = "Yadda"
End If
End Sub
Putting the cursor in a paragraph (any paragraph) and repeatedly hitting Alt-j will "toggle" the paragraph style back and forth between "Yadda" and "Blah"

As this is moving into VBA, perhaps it would be better to make a thread in the VBA forum if you need more help with this.

I will reiterate though that I suspect this is not the best way to use Word. If your shortcuts are formatting parts of paragraphs, then it would be better to use Character styles, rather than what is actually manual formatting.


faq219-2884

Gerry
My paintings and sculpture
 

Gerry,

Please clarify. I do not understand what you mean by "at least not in the "Style" section where shortcut keys are defined".

I think I know what bartoshw meant. Not sure where to find this shortcut key definition in Word 2007, but it is probably similar to my Word 2000. In MS Word 2000, assignment of shortcut keys can be made in two ways (that I am aware off). One, Tools -> Customize -> Keyboard -> Categories (Styles, for Example) -> Shortcut Key. The other one is Format -> Style -> Modify -> Shortcut Key.

Of course, it is most likely stored as a Sub somewhere, but the OP doesn't know where to find the source code to modify it. (At a quick look, it wasn't readily available in VB Editor as a Sub within the template file.)
 

bartoshw

As an easy fix, you can try to assign another shortcut key to the Normal style - but of course it is nice to find the code and make them toggle.
 
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
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top