I am creating an Excel Add-In using Visual Studio 2010. I would like to run some code when users clicks a combination of keys.
Here is the code I have got
Public Class CC
Private Sub ThisAddIn_Startup() Handles Me.Startup
EnableShortCut()
End Sub
Sub A1()
MsgBox "A1"
End Sub
Sub A2()
MsgBox "A2"
End Sub
Sub A3()
MsgBox "A3"
End Sub
Public Sub EnableShortCut()
With Application
.OnKey "+^{U}", "A1" 'action A1 should be performed when user clicks Ctrl + Shift + U
.OnKey "+^{L}", "A2" 'action A2 should be performed when user clicks Ctrl + Shift + L
.OnKey "+^{P}", "A3" 'action A3 should be performed when user clicks Ctrl + Shift + P
End With
End Sub
End Class
The Add-In when installed shows an error on clicking the short cuts. It says the specific macro cannot be found.
The code under the Sub EnableShortCut() works well when it is in an excel vba module. The same won’t work when it is added to an Excel Add-In created with Visual Studio.
Some one out there please help me to resolve this issue.
“I would like to run some code when users presses a combination of keys.”
Its tricky and to do it without any external dependencies resort to Keyboard hooking to achieve it with a VSTO Excel Add-in:
You’ll need to put code in the HookCallback() method in the above code to trap events when key combinations are pressed, I’ve given you two examples Ctrl + Shift + 7 and Ctrl + 7 to get you going.
Then in your Excel AddIn wire it up:
And dont forget to disable it when your done: