How to perform .Onkey Event in an Excel Add-In created with Visual Studio 2010?

后端 未结 4 649
被撕碎了的回忆
被撕碎了的回忆 2020-12-03 18:23

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

         


        
4条回答
  •  余生分开走
    2020-12-03 18:53

    This is difficult to do, the Application.OnKey() method is very restricted. It can only call a macro and cannot pass any arguments. Which means that you'll have to provide a set of macros. You don't want workbook specific ones, you need macros that work in any document. Let's tackle that first.

    • Start Excel and use File + Close to close the default workbook
    • Click Record macro. Change the "Store macro in" setting to Personal Macro Workbook
    • OK to close the dialog and click Stop Recording
    • Click Visual Basic. Note that you've now got a VBA project named PERSONAL.XLSB with a Module1

    Delete Macro1 and copy/paste this VBA code:

    Sub MyAddinCommand1()
      Application.COMAddIns("ExcelAddin1").Object.Command 1
    End Sub
    
    Sub MyAddinCommand2()
      Application.COMAddIns("ExcelAddin1").Object.Command 2
    End Sub
    
    Sub MyAddinCommand3()
      Application.COMAddIns("ExcelAddin1").Object.Command 3
    End Sub
    

    Repeat as often as necessary, you want one for each shortcut key you want to define. Click Save. You now created a file in c:\users\yourname\appdata\roaming\microsoft\excel\xlstart\personal.xlsb that contains the macros. Anybody that is going to use your extension needs to have this file as well, a deployment detail.

    Next thing you need to do is expose your commands. The Sub A1() you wrote in the addin will not do, the methods need to be exposed as methods of a COM visible class. Add a new class to your project and make the code look like this:

    Imports System.Runtime.InteropServices
    
     _
     _
    Public Interface IMyAddinCommand
        Sub Command(ByVal index As Integer)
    End Interface
    
     _
     _
    Public Class MyAddinCommand
        Implements IMyAddinCommand
    
        Public Sub Command(index As Integer) Implements IMyAddinCommand.Command
            MsgBox("Command #" + CStr(index))
        End Sub
    End Class
    

    Just a simple one that exposes a single method named Command() that takes an integer. I'm just used MsgBox, you'll want to write a Select statement to implement the command based on the value of index. Also note the match with the code in the global macro.

    One more thing you need to do, you must expose this class explicitly. Override the RequestComAddInAutomationService function in your addin. The one I used to test this looked like this:

    Public Class ThisAddIn
    
        Private Sub ThisAddIn_Startup() Handles Me.Startup
            Application.OnKey("+^{U}", "Personal.xlsb!MyAddinCommand1")  '' Ctrl + Shift + U
        End Sub
    
        Protected Overrides Function RequestComAddInAutomationService() As Object
            If commands Is Nothing Then commands = New MyAddinCommand
            Return commands
        End Function
    
        Private commands As MyAddinCommand
    
    End Class
    

    Press F5 to compile and start Excel. When I press Ctrl+Shift+U I get this:

    enter image description here

提交回复
热议问题