使用VBA在Word中创建键盘热键/快捷方式

时间:2017-03-31 14:03:18

标签: vba ms-word keyboard-shortcuts word-vba

由于某些原因,我的normal.dotm文件中的快捷方式偶尔会被删除,
所以我知道我可以通过单击单词(https://wordribbon.tips.net/T008058_Assigning_a_Macro_to_a_Shortcut_Key.html

来创建单词的快捷方式

但是我想像在Excel中那样对它们进行硬编码:

Sub keyBoardShortCuts()

    Application.OnKey "%^d", "updateDB"
    Application.OnKey "%^p", "openProjectList"
    Application.OnKey "%^m", "addNewLS"
    Application.OnKey "%^l", "createLS"
    Application.OnKey "%^g", "loadGui"
    Application.OnKey "%^b", "Custom_Button_Click"

End Sub
Private Sub Workbook_Open()
    Call keyBoardShortCuts
End Sub

然而,这在单词中不起作用,那么在单词中这样做的语法是什么?

1 个答案:

答案 0 :(得分:2)

您需要使用KeyBindingsEnum WdKey中的常量(按F2查看列表):

With Application.KeyBindings
    .Add KeyCode:=BuildKeyCode(wdKeyAlt, wdKeyControl, wdKeyD), _
         KeyCategory:=wdKeyCategoryCommand, _
         Command:="updateDB"
    .Add KeyCode:=BuildKeyCode(wdKeyAlt, wdKeyControl, wdKeyP), _
         KeyCategory:=wdKeyCategoryCommand, _
         Command:="openProjectList"
    .Add KeyCode:=BuildKeyCode(wdKeyAlt, wdKeyControl, wdKeyM), _
         KeyCategory:=wdKeyCategoryCommand, _
         Command:="addNewLS"
    '...
End With 'Application.KeyBindings