xlcOnKey

577 views
Skip to first unread message

RS DID

unread,
Nov 2, 2010, 1:07:04 PM11/2/10
to Excel-Dna
Hi,
I am wondering if I can use the xlcOnKey to call a macro defined in
XLL. I have tried to set this up by doing :

XlCall.Excel(XlCall.xlcOnKey, @"%X", "VectorResize");

Where VectorResize is defined in the XLL code as

[ExcelCommand(Name="VectorResize")]
public static void VectorResize()
{
MessageBox.Show("Hello");

}


I have a message box just to see if I could get the code to execute,
but the line where I define the xlcOnKey is throwing an exception.

Any help will be much appreciated.

RSD

Govert van Drimmelen

unread,
Nov 2, 2010, 1:47:33 PM11/2/10
to Excel-Dna
Hi RSD,

Firstly, your call looks fine and I don't get an error when I try it.
What exception do you get? You can also use the XlCall.TryExcel(...)
variant, which will return a result code of type XlCall.XlResult,
perhaps giving us an indication of the error. You won't be able to
call xlcOnKey from inside a function.

Secondly, on my Excel the Alt+Shift+X (%X) wouldn't work right - my
Excel 2007 tries to do some menu command. I think Alt is tricky -
maybe you should try something like "^G", which is Ctrl+Shift+G and
not bound to anything else. (Though "%G" worked, as did "{CAPSLOCK}".)

The .dna file below worked fine for me.

Regards,
Govert

<DnaLibrary Name="Test OnKey" Language="VB">
<Reference Path="System.Windows.Forms.dll" />
<![CDATA[
Imports System.Windows.Forms

Public Module Test

<ExcelCommand(MenuText:="Show Excel Version")> _
Public Sub ShowVersion()
MessageBox.Show(ExcelDnaUtil.Application.Version)
End Sub

<ExcelCommand(MenuText:="Install OnKey Handler")> _
Public Sub InstallOnKey()
Try
XlCall.Excel(XlCall.xlcOnKey, "^G", "ShowVersion")
MessageBox.Show("OnKey handler installed.")
Catch e as Exception
MessageBox.Show("Error trying to install OnKey handler: "
& e.ToString())
End Try
End Sub

End Module

]]>
</DnaLibrary>

RS DID

unread,
Nov 2, 2010, 1:58:39 PM11/2/10
to exce...@googlegroups.com
Govert,
Thanks.  I was trying to set the xlcOnKey in a function call.  When I moved it out to the Auto-Open it worked. So just as you suspected, I don't think this shortcut can be registered in an Excel function.  I did take your suggestion and changed the key shortcut to use CTRL in place of ALT.

RSD

--
You received this message because you are subscribed to the Google Groups "Excel-Dna" group.
To post to this group, send email to exce...@googlegroups.com.
To unsubscribe from this group, send email to exceldna+u...@googlegroups.com.
For more options, visit this group at http://groups.google.com/group/exceldna?hl=en.


Ian Murphy

unread,
Apr 9, 2014, 12:25:37 PM4/9/14
to exce...@googlegroups.com
This is a rather old thread so apologies for reviving it. I'm trying to do something similar (capture the F9 key) and I get an error saying that the macro doesn't exist or macros are disabled.

so, I tried copying and pasting your chunk of code and it results in the same problem when it tries to locate ShowVersion.

I've tried combinations of declaring the functions explicitly as macros, changing the names, using different keys... the resul is always the same. When I press the key sequence Excel reports "Cannot run the macro "showversion". The macro may not be available in this workbook or all macros may be disabled"
This is with office 2010. I've tried enabling macros from options>Confidence center > Config > Enable all macros (not recommended)
but this had no effect.
If I open the macro editor I don't see anything in the list of known macros - should the list be empty?
I've also tried declaring the functions as 
<ExcelCommand(Name:="Recalc")
and
<ExcelFunction( Name:="Recalc")

Any suggestions as to what might be wrong?

Ian Murphy

unread,
Apr 9, 2014, 1:09:37 PM4/9/14
to exce...@googlegroups.com
I just worked this out for myself. After several revisits to the subject over a few months I finally clicked - the functions should have been registered as shared - which is why they were not being picked up as macros.


Govert van Drimmelen

unread,
Apr 9, 2014, 1:31:09 PM4/9/14
to <exceldna@googlegroups.com>
Hi Ian,

The Alt+F8 will be empty, but if you type in the name, the Run button will light up and you press it.

Your macro must be 'public static void'.

With recent Excel-DNA versions you can add the Shortcut in the attribute:

<ExcelCommand(ShortCut:="^D")>

-Govert


--
You received this message because you are subscribed to the Google Groups "Excel-DNA" group.
To unsubscribe from this group and stop receiving emails from it, send an email to exceldna+u...@googlegroups.com.

To post to this group, send email to exce...@googlegroups.com.
Visit this group at http://groups.google.com/group/exceldna.
For more options, visit https://groups.google.com/d/optout.

Thang Trinh Xuan

unread,
Apr 24, 2014, 11:11:25 AM4/24/14
to exce...@googlegroups.com
Hi Govert

How can I set shortcut is Ctrl+T or Alt+T

Thanks and regards,

Vào 00:07:04 UTC+7 Thứ tư, ngày 03 tháng mười một năm 2010, RSD đã viết:

Govert van Drimmelen

unread,
Apr 24, 2014, 11:28:25 AM4/24/14
to exce...@googlegroups.com

Hi,

 

I don’t think you can register Alt+t, since it’s used by the menu system. Ctrl+t and Alt+b work, though.

 

You can either set up the shortcut in code, or in an ExcelCommand attribute. I paste some code below.

 

-Govert

 

using ExcelDna.Integration;

 

public class Test : IExcelAddIn

{

    public void AutoOpen()

    {

        // Register Ctrl+t to call SayHello

        XlCall.Excel(XlCall.xlcOnKey, "^t", "SayHello");

    }

   

    public void AutoClose()

    {

        // Clear the registration if the add-in is unloaded

        XlCall.Excel(XlCall.xlcOnKey, "^t");

    }

 

    [ExcelCommand(MenuText = "Say Hello")]

    public static void SayHello()

    {

        XlCall.Excel(XlCall.xlcAlert, "Hello there!");

    }

   

    // ShortCut from attribute  requires Excel-DNA 0.32

    // "%b" means Alt+b

    [ExcelCommand(MenuText = "Say Boo", ShortCut="%b")]

    public static void SayBoo()

    {

        XlCall.Excel(XlCall.xlcAlert, "Boo!");

    }

}

Thang Trinh Xuan

unread,
Apr 25, 2014, 2:49:08 AM4/25/14
to exce...@googlegroups.com
It's worked, thank you!


Vào 00:07:04 UTC+7 Thứ tư, ngày 03 tháng mười một năm 2010, RSD đã viết:
Hi,
Reply all
Reply to author
Forward
0 new messages