View Single Post
  #2   Report Post  
Posted to microsoft.public.excel.programming
GS GS is offline
external usenet poster
 
Posts: 364
Default How to map a shortcut key to a routine in COM AddIn for Excel 2007

buddylake used his keyboard to write :
1. Background:

I have a COM addins developed in Visual basic 6.0 for EXCEL 2007. The COM
addin has its toolbar visible. It exposes customized 'Copy' as a button on
the toolbar, which will call MyCopyAction routine in the COM addin.

2. What I want:

I want to map a shortcut key for the customized 'copy' operation, which
indeed invoke the same routine as that on toolbar, i.e. MyCopyAction.

3. What I tried in the COM addin:

I added the following line in

Private Sub AddinInstance_OnStartupComplete(custom() As Variant)

...

Application.OnKey "+^c", "MyCopyAction"
...

End Sub

Problem was: Message box from EXCEL "cannot run the macro MyCopyAction ...".
MyCopyAction is not a macro. It's a routine in the COM AddIn. What options
do I have to make it work?

Thanks!


I use COMAddins but I don't build my menus there; I use a xla for this
and redirect all controls' OnActions through a single procedure that
serves as an entry point to the COMAddin's procedures. It uses the
appropriate calls from within Excel to use the procedures inside the
COMAddin. (No code is in the xla other than what's required to
create/remove menus/toolbars.

The first thing I see in your code is that Application is not referring
to your COMAddin. (Assumes you have 'Set' a global variable initialized
to ref Excel in the OnConnection routine) So.., if appXL is your
COMAddin's global variable that holds its ref to Excel then the line of
code from your COMAddin should be:

appXL.OnKey...

Also, I don't see where you tell Excel that "MyCopyAction" is located
in your COMAddin. In this case, I think it will have to be a public
method in order for Excel to access it. I know that from Excel, we must
ref the COMAddin same as we would ref executing a macro in another
workbook:

Application.COMAddIns(gsAPP_NAME & ".Connect").Object.MyCopyAction

Here again, you need to replace 'Application' with your object var. So
give this a try:

With appXL
.OnKey "+^c", _
.COMAddins("gsAPP_NAME & ".Connect").Object.MyCopyAction
End With

I don't know if it will work because I set these up in my xla file to
go through the common entry point procedure.

Good luck
Garry
--