Results 1 to 1 of 1

Thread: [FAQ's: OD] How do I make a HotKey (shortcut keypress) in Outlook?

  1. #1

    Thread Starter
    Ex-Super Mod RobDog888's Avatar
    Join Date
    Apr 2001
    Location
    LA, Calif. Raiders #1 AKA:Gangsta Yoda™
    Posts
    60,709

    [FAQ's: OD] How do I make a HotKey (shortcut keypress) in Outlook?

    When you create custom Menu Items and Toolbar Buttons you may find yourself needing to associate a shortcut key or keypress sequence to invoke your custom click event. Using the RegisterHotkey API call as well as a few others you can make it work.

    For ex: You added a custom menut item ...

    "My Send Button (Ctl+Alt+S)"

    When clicked, the menu will fire the click event when you set the .OnAction property of the menu item or sync up a WithEvents. The shortcut is different.

    We use the RegisterHotKey API just as we would in any other programming language but we need a parent window handle and Outlook doesnt expose one to us from the Application object like it does in a few other Office Apps. Using the FindWindow API we can get the window handle and progress as usual.

    The Application_MAPILogonComplete() event is where we need to start initializing. Then to clean up when Outlook closes we need to unhook the hotkey in the Application_Quit event.

    Using this method and APIs we can assign shortcut keys or shortcut keypress sequences that can not be achieved in a menu editor.

    For this example we will use "Ctl+Alt+S". This will also show that you can override the default shortcut hotkeys and replace them with your own event.


    Outlook 2003 Or 2007 VBA:

    vb Code:
    1. 'BEHIND CLASS MODULE: "ThisOutlookSession"
    2. Option Explicit
    3. 'Copyright © 2007 by RobDog888 (VB/Office Guru™). All Rights reserved.
    4. '
    5. 'Distribution: You can freely use this code in your own
    6. '              applications provided that this copyright
    7. '              is left unchanged, but you may not reproduce
    8. '              or publish this code on any web site, online
    9. '              service, or distribute as source on any
    10. '              media without express permission.
    11. Private Type POINTAPI
    12.     x As Long
    13.     y As Long
    14. End Type
    15.  
    16. Private Type Msg
    17.     hWnd As Long
    18.     Message As Long
    19.     wParam As Long
    20.     lParam As Long
    21.     time As Long
    22.     pt As POINTAPI
    23. End Type
    24.  
    25. Private Declare Function RegisterHotKey Lib "user32.dll" ( _
    26.                         ByVal hWnd As Long, _
    27.                         ByVal id As Long, _
    28.                         ByVal fsModifiers As Long, _
    29.                         ByVal vk As Long) As Long
    30.  
    31. Private Declare Function UnregisterHotKey Lib "user32" ( _
    32.                         ByVal hWnd As Long, _
    33.                         ByVal id As Long) As Long
    34.  
    35. Private Declare Function PeekMessage Lib "user32" Alias "PeekMessageA" ( _
    36.                         lpMsg As Msg, _
    37.                         ByVal hWnd As Long, _
    38.                         ByVal wMsgFilterMin As Long, _
    39.                         ByVal wMsgFilterMax As Long, _
    40.                         ByVal wRemoveMsg As Long) As Long
    41.                        
    42. Private Declare Function WaitMessage Lib "user32" () As Long
    43.  
    44. Private Declare Function FindWindow Lib "user32.dll" Alias "FindWindowA" ( _
    45.                         ByVal lpClassName As String, _
    46.                         ByVal lpWindowName As String) As Long
    47.  
    48. Private Const MOD_ALT = &H1
    49. Private Const MOD_CONTROL = &H2
    50. Private Const MOD_SHIFT = &H4
    51. Private Const PM_REMOVE = &H1
    52. Private Const WM_HOTKEY = &H312
    53.  
    54. Private mbCancel As Boolean
    55. Private lHwnd As Long
    56.  
    57. Private Sub ProcessMessages()
    58.     Dim Message As Msg
    59.     'LOOP UNTIL MBCANCEL IS TRUE
    60.     Do While Not bCancel
    61.         'WAIT FOR MESSAGE
    62.         WaitMessage
    63.         'CHECK IF ITS A HOTKEY MESSAGE
    64.         If PeekMessage(Message, lHwnd, WM_HOTKEY, WM_HOTKEY, PM_REMOVE) Then
    65.             'INVOKE THE SEND MACRO OF THE SELECTED ITEM
    66.             MsgBox "Ctl+Alt+S", vbOKOnly + vbInformation, "OD FAQ TEST"
    67.         End If
    68.         'RESUME THE OS PROCESSING OF OTHER MESSAGES
    69.         DoEvents
    70.     Loop
    71. End Sub
    72.  
    73. Private Sub Application_MAPILogonComplete()
    74.     Dim ret As Long
    75.     mbCancel = False
    76.     Dim iBuild As Integer
    77.     'VERSION DETECTION FROM MY OUTLOOK FAQ:
    78.     'http://vbforums.com/showthread.php?t=402020
    79.     'GET THE MAJOR BUILD ONLY TO MAKE IT EASIER TO DETERMINE EXACT VERSION
    80.     iBuild = Left$(Application.Version, InStr(1, Application.Version, ".") + 1)
    81.     Select Case iBuild
    82.         Case 7 To 10
    83.             'sVersion = "97/98/2000/2002"
    84.             MsgBox "Too Old! Just kidding." & vbNewline & _
    85.             "I dont have the older versions to test with.", _
    86.             vbOkOnly + vbInformation, "[FAQ's: OD] Outlook HotKey"
    87.             Exit Sub
    88.         Case 11, 12
    89.             'sVersion = "2003/2007"
    90.             lHwnd = FindWindow("rctrl_renwnd32", "Inbox - Microsoft Outlook")
    91.         Case Else
    92.             MsgBox "Too New!"
    93.             Exit Sub
    94.     End Select
    95.     'NO NEED TO ERROR TRAP FOR THE OUTLOOK WINDOW SINCE THIS IS VBA CODE
    96.     lHwnd = FindWindow("rctrl_renwnd32", "Inbox - Microsoft Outlook")
    97.     'REGISTER THE HOTKEY SEQUENCE: Ctrl+Alt+S
    98.     ret = RegisterHotKey(lHwnd, &HBFFF&, MOD_CONTROL Or MOD_ALT, vbKeyS)
    99.     'PROCESS THE HOTKEY MESSAGES
    100.     ProcessMessages
    101. End Sub
    102.  
    103. Private Sub Application_Quit()
    104.     mbCancel = True
    105.     'UNREGISTER HOTKEY
    106.     Call UnregisterHotKey(lHwnd, &HBFFF&)
    107. End Sub
    Last edited by RobDog888; Apr 3rd, 2007 at 10:12 PM.
    VB/Office Guru™ (AKA: Gangsta Yoda®)
    I dont answer coding questions via PM. Please post a thread in the appropriate forum.

    Microsoft MVP 2006-2011
    Office Development FAQ (C#, VB.NET, VB 6, VBA)
    Senior Jedi Software Engineer MCP (VB 6 & .NET), BSEE, CET
    If a post has helped you then Please Rate it!
    Reps & Rating PostsVS.NET on Vista Multiple .NET Framework Versions Office Primary Interop AssembliesVB/Office Guru™ Word SpellChecker™.NETVB/Office Guru™ Word SpellChecker™ VB6VB.NET Attributes Ex.Outlook Global Address ListAPI Viewer utility.NET API Viewer Utility
    System: Intel i7 6850K, Geforce GTX1060, Samsung M.2 1 TB & SATA 500 GB, 32 GBs DDR4 3300 Quad Channel RAM, 2 Viewsonic 24" LCDs, Windows 10, Office 2016, VS 2019, VB6 SP6

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width