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

发布于 2024-12-17 18:14:31 字数 106 浏览 0 评论 0原文

标题几乎是

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

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

听风念你 2024-12-24 18:14:31

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 

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 
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文