且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

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

更新时间:2023-11-17 14:33:10

KeyBindings对象应该可以解决问题.在此处查看示例: http://www.vbaexpress.com/kb/getarticle. php?kb_id = 621

The KeyBindings object should do the trick. See an example here: 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