Assign customized shortcut to a marco in worksheet not module

Vincent88

Active Member
Joined
Mar 5, 2021
Messages
382
Office Version
  1. 2019
Platform
  1. Windows
  2. Mobile
I have the following script to put in sheet not module. The marco works when I run by press ATL F8 but not the shortcut (received error). How can I modify it to make it to run with shortcut key as well.

Sub CreateShortcutMsg ()
Application.OnKey "^{98}", OpenOutlookTemplate"

End Sub

Sub DeleteShortcutMsg ()
Application.OnKey "^{98}", OpenOutlookTemplate"

End Sub

Sub OpenOutlookTemplate()
If ActiveWorkbook Is ThisWorkbook Then

Dim myoutapp As Object
Dim myitem As Object
Set myoutapp = CreateObject("Outlook.Application")
Set myitem = myoutapp.CreateItemFromTemplate("C:\Users\vincentszeto\Desktop\MrExcel.msg")
myitem.Display
End If

End Sub

Error when press the shortcut key
1615205386119.png
 

Excel Facts

Last used cell?
Press Ctrl+End to move to what Excel thinks is the last used cell.
Why is the code in a worksheet module? It really doesn't belong there.

Anyway, you need to include the codename of the sheet in the macro name - eg:

Rich (BB code):
Sub CreateShortcutMsg()
Application.OnKey "^{98}", "Sheet1.OpenOutlookTemplate"

End Sub

Sub DeleteShortcutMsg()
Application.OnKey "^{98}"

End Sub

Note also the amendment to the code to reset the shortcut.
 
Upvote 0

Forum statistics

Threads
1,214,887
Messages
6,122,095
Members
449,064
Latest member
Danger_SF

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top