如何在Word VBA中创建hotKey(以编程方式)?

Ben*_*Ben 1 vba ms-word

标题几乎是个问题

我想制作一个便携式热键,这意味着如果我粘贴vba代码这个词仍然可以使用那个热键,比如Excel的 application.onkey

Rac*_*ger 7

KeyBindings对象应该做到这一点.请参阅此处的示例:http://www.vbaexpress.com/kb/getarticle.php?kb_id = 621

' \\ Code for Module1
Option Explicit 

Sub AddKeyBinding() 
    With Application 
         ' \\ Do customization in THIS document
        .CustomizationContext = ThisDocument 

         ' \\ Add keybinding to this document Shorcut: Alt+0
        .KeyBindings.Add KeyCode:=BuildKeyCode(wdKeyAlt, wdKey0), _ 
        KeyCategory:=wdKeyCategoryCommand, _ 
        Command:="TestKeybinding" 
    End With 
End Sub 

 ' \\ Code for Module2
Option Explicit 

 ' \\ Test sub for keybinding
Sub TestKeybinding() 
    MsgBox "We have a winner", vbInformation, "Succes" 
End Sub 
Run Code Online (Sandbox Code Playgroud)

  • 仅链接答案为-1.链接只有答案是坏的,一直都是,永远都会.如果链接已经死亡,那将毫无用处,那又怎样呢?什么都没有的链接.所以,我复制了相关的部分......虽然它解决了当时的答案,如果链接发生了,它将来也不会帮助其他人...但是,我删除了downvote (2认同)