Is there a way to capture HotKeys/Shortcuts in Excel VSTO using only C# and no VBA?

混江龙づ霸主 提交于 2019-12-22 04:44:16

问题


So I want to capture some key-commands in our Docuement-level Excel VSTO addin. I can't seem to find a way to do it, other than to use VBA and have our addin talk to the VBA. Any help/examples would be greatly appreciated.

I am using Excel 2007.


回答1:


You can only do this through API calls to subclass Excel and watch for key commands. This is older, but it still applies.




回答2:


  1. One method involves using the 3rd party solution from Addin-Express. Their product includes the ability to add a keyboard shortcut as a property to the ribbon menu commands.

  2. The other way is to make use of low level keyboard hooks, through some Win32 API's which is generally referred to as windows subclassing. Here is an excellent explanation with code sample of how to do it. Note that the only "extra" thing you need to do to get this code to "work" in VSTO is moving the SetHook() method to the Startup event, and the UnhookWindowsHookEx() method to the Shutdown event.

    Check out the article on MSDN here by Stephen Toub.

  3. Finally there is the use of the OnAction property of the Addin class. This method requires the use of some VBA (in terms of a callback method that points back to the underlying .net addin), and works ok so long as you are willing to distribute some VBA in your solution (i.e. a xls or doc w/ vba project, or perhaps a native addin). Note you will also need to mark comvisible = true, and expose the GetAutomationServiceObject method so that your VBA can reference your addin from VBA code.

    see here for a thread on it...



来源:https://stackoverflow.com/questions/2297839/is-there-a-way-to-capture-hotkeys-shortcuts-in-excel-vsto-using-only-c-sharp-and

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!