Results 1 to 3 of 3
  1. #1
    5 Star Lounger
    Join Date
    Mar 2001
    Location
    New York, NY
    Posts
    922
    Thanks
    2
    Thanked 12 Times in 11 Posts

    Tab Key - Keybinding

    We want to run a macro when the TAB key is hit, for documents based on one template. The main purpose of this is to prevent adding a new row if TAB is hit when in the last cell. (This was possible in WordPerfect, and that is where the notion comes from.)

    I have the macro code that functions well. I realize that it's necessary to use VBA code, as TAB cannot be reassigned with the Keyboard Customization feature.

    I've seen sample code using KeyBindings.AddKeycode. I can use CustomizationContext = ActiveDocument.Attached template to restrict the assignment to a particular template. If I'm writing the code in that template, is it possible to specify the current template?

    My main concern is how to remove the assignment if we decide we don't want it any longer. There's a ClearAll method, but is there a way to remove the assignment for the TAB key only?

    I assume there's a good reason for not making it easy to assign a macro to the TAB key, but we are determined. I will appreciate any insight and/or caveats.

  2. #2
    Super Moderator jscher2000's Avatar
    Join Date
    Feb 2001
    Location
    Silicon Valley, USA
    Posts
    23,112
    Thanks
    5
    Thanked 93 Times in 89 Posts
    Quote Originally Posted by richardbarrett View Post
    I've seen sample code using KeyBindings.AddKeycode. I can use CustomizationContext = ActiveDocument.Attached template to restrict the assignment to a particular template. If I'm writing the code in that template, is it possible to specify the current template?
    If you search for MacroContainer, you should find examples of how a procedure can get a reference to its containing document without having to know in advance whether it is in a document or template. There was another way as well, but I have forgotten the details on both.

    Quote Originally Posted by richardbarrett View Post
    My main concern is how to remove the assignment if we decide we don't want it any longer. There's a ClearAll method, but is there a way to remove the assignment for the TAB key only?
    Yes, at least according to an August 2003 post:

    Code:
    Sub ClearKeyBinding()
    ' Hardcoded to clear keybinding for the letter A (unshifted)
    Dim kb As KeyBinding
    ' Set context as desired - customize as needed
    CustomizationContext = NormalTemplate
    ' Create object reference to keycode (works with or without
    '  an existing key binding for the command key)
    Set kb = FindKey(BuildKeyCode(wdKeyA))
    If kb.KeyCategory = wdKeyCategoryNil Then
        MsgBox "No such key assignment; no action taken"
    Else
        kb.Clear
        MsgBox "Key assignment cleared"
    End If
    Set kb = Nothing
    End Sub
    Alternately, in 2002, Rory posted code to unmap any keys assigned to a particular macro:

    Code:
        For Each myKey In Application.KeysBoundTo(wdKeyCategoryCommand, "MyProcedureName")
            myKey.Clear
        Next myKey
    One of those hopefully will work for you.

    Quote Originally Posted by richardbarrett View Post
    I assume there's a good reason for not making it easy to assign a macro to the TAB key, but we are determined. I will appreciate any insight and/or caveats.
    There is a risk that it will fire while navigating dialogs, i.e., well beyond the scope of the table. Error trapping (e.g., for .Information() being undefined) may solve this problem, but I certainly have not tested it.

    To read a long thread about experiments with the spacebar, see: Monitoring typing (Word97SR2) @ Windows Secrets Lounge.

  3. The Following User Says Thank You to jscher2000 For This Useful Post:

    RetiredGeek (2011-03-20)

  4. #3
    5 Star Lounger
    Join Date
    Mar 2001
    Location
    New York, NY
    Posts
    922
    Thanks
    2
    Thanked 12 Times in 11 Posts
    I hadn't thought about possible problems in dialog boxes. This template has no custom dialog boxes, but I'll test thoroughly with Word dialog boxes. I fear we may be over-engineering with this, and I plan to be very caution.

    Many thanks for your reply.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •