2017-03-31 127 views
1

出於某種原因,我在我的normal.dotm文件的快捷方式獲取了一會兒刪除一次,
所以我知道我可以通過點擊單詞圍繞在Word中創建快捷方式(https://wordribbon.tips.net/T008058_Assigning_a_Macro_to_a_Shortcut_Key.html創建鍵盤熱鍵/在Word快捷鍵使用VBA

,但我想他們硬編碼,而不是像我在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

你需要使用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