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

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/8224480/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-11 14:31:23  来源:igfitidea点击:

How to create hotKey in Word VBA (programatically)?

vbams-word

提问by Ben

The title is pretty much the question

标题几乎就是问题

I want to make a portable hotkey, meaning if I paste the word vba code I can still use that hotkey, something like Excel's application.onkey

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

回答by Rachel Hettinger

The KeyBindingsobject should do the trick. See an example here: http://www.vbaexpress.com/kb/getarticle.php?kb_id=621

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