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

时间:2011-11-22 09:25:03

标签: vba ms-word

标题几乎就是问题

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

1 个答案:

答案 0 :(得分: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