Consulting

Results 1 to 6 of 6

Thread: Assigning a key combination to a macro by code

Hybrid View

Previous Post Previous Post   Next Post Next Post
  1. #1
    VBAX Regular
    Joined
    Oct 2006
    Posts
    12
    Location

    Assigning a key combination to a macro by code

    A have a macro, that I would like to run by pressing two keys of my keyboard simultaneously , for example Alt+Enter, because it is so convenient. Ideally this shortcut would be ?alive? only while I am working with a certain document, on closing the document it should be cancelled, for in other macros I use the same shortcut for different purposes. In Excel I used the OnKey statement for watching keystrokes. Interestingly enough in Word OnKey doesn?t work. Instead there is a much more complex way to assign keystroke combinations to macros. My code is like this:

    PHP Code:
    Sub hidereveal()
    Dim hidrange As Range
    CustomizationContext 
    ActiveDocument
    KeyBindings
    .Add KeyCode:=BuildKeyCode(wdKeyAltwdKeyReturn), KeyCategory:=wdKeyCategoryMacro_
    Command
    :="revrange"
    Set hidrange ActiveDocument.Range(Start:=Selection.EndEnd:=ActiveDocument.Range.End)
    hidrange.Font.Color RGB(160160164)
    ActiveDocument.Range(Start:=0End:=Selection.End).Font.Color wdColorAutomatic
    End Sub 

    Lines 3 and 4 are relavant to my question. When I run the macro invariably I get a run-time error nr ?5346? and the message: The function assigned to the specified key cannot be modified. I tried different key combinations, or single keys, but could not find any that would not trigger the same run-time error. Can anyone help me with this issue. I have reached the point where I have no more ideas.


  2. #2
    Administrator
    VP-Knowledge Base
    VBAX Grand Master mdmackillop's Avatar
    Joined
    May 2004
    Location
    Scotland
    Posts
    14,489
    Location
    If you save your code in a module within the Document, and also save your shortcut there, it would then be usable as you require
    MVP (Excel 2008-2010)

    Post a workbook with sample data and layout if you want a quicker solution.


    To help indent your macros try Smart Indent

    Please remember to mark threads 'Solved'

  3. #3
    VBAX Wizard
    Joined
    May 2004
    Posts
    6,713
    Location
    I am not sure what the problem is. I can get it to work.[vba]Option Explicit

    Sub NewTest()
    MsgBox "Hello"
    End Sub

    Sub Whatever()

    CustomizationContext = ActiveDocument
    KeyBindings.Add KeyCode:=BuildKeyCode(wdKeyAlt, wdKeyReturn), _
    KeyCategory:=wdKeyCategoryMacro, _
    Command:="NewTest"

    End Sub[/vba] I used this as the stuff with the Range is not - or shouldn't be - relevant to the issue.

    I ran the Whatever Sub. Alt-Enter now fires the Sub NewTest, and the message is displayed.

    Alt-Enter, BTW, is the built-in default Word key combination for ReDo.

    Not only that, both Subs above are in Normal.dot. I fired it, then saved the current document. The Alt-Enter (to display "Hello") key combination is only available in that document. In any other document Alt-Enter acts like normal...it fires ReDo.

  4. #4
    VBAX Regular
    Joined
    Oct 2006
    Posts
    12
    Location
    Thank you for your replies. I have learnt new things from them, the solution to my problem was much more simple, though. I simply mistyped the called procedure. The keybinding command works allright. It was a human mistake this time.

  5. #5
    VBAX Wizard
    Joined
    May 2004
    Posts
    6,713
    Location
    Question: are you using Option Explicit?

  6. #6
    VBAX Regular
    Joined
    Oct 2006
    Posts
    12
    Location
    Yes, I always use option exlicit. It helps you with variables, but not with mistyped procedure names, as far as I know.

Posting Permissions

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