简体   繁体   English

如何使用 ExcelDna 中的 ExcelCommand

[英]How to use ExcelCommand from ExcelDna

I have a simple ExcelCommand:我有一个简单的 ExcelCommand:

[ExcelCommand(Name = "MyTestCommand", ShortCut = "^Q")]
public static void Teste()
{
    var xlApp = (Application)ExcelDnaUtil.Application;
    var ws = xlApp.Sheets[1] as Worksheet;
    var range = ws.Cells[1, 1] as Range;
    range.Value2 = "foo bar";
}

When I press Ctrl + Shift + Q , cell A1 from the first sheet receives the text "foo bar" .当我按Ctrl + Shift + Q 时,第一张工作表中的单元格 A1 收到文本"foo bar"

The client doesn't want a shortcut, he wants a user interface button (in Ribbon or in sheet body, doesn't matter).客户不想要快捷方式,他想要一个用户界面按钮(在功能区或工作表正文中,无关紧要)。

With VBA I can write:用 VBA 我可以写:

Sub Button1_OnClick()
    MyTestCommand
End Sub

But call MyTestCommand doesn't work.但是调用MyTestCommand不起作用。

How can I call my created command?如何调用我创建的命令?

If you set a value for the MenuText member of ExcelCommand , it will automatically become available through the Add-Ins ribbon.如果您为ExcelCommandMenuText成员设置了一个值,它将通过Add-Ins功能区自动变为可用。

在此处输入图片说明

[ExcelCommand(MenuText = "My Test Command", ShortCut = "^Q")]
public static void Teste()
{
    // ...
}

If you want more control over the look-and-feel of how the command is presented, then create your own CustomUI , with your own button, icon, etc. Take a look at the different Ribbon examples that come with Excel-DNA .如果您想更好地控制命令呈现方式的CustomUI ,请使用您自己的按钮、图标等创建您自己的CustomUI查看 Excel-DNA 附带的不同 Ribbon 示例

There is a VERY horrid frig that you could employ by using VBA's quirky SendKeys function.您可以使用 VBA 古怪的 SendKeys 函数来使用一个非常可怕的 frig。 This would get things working without having to use the ribbon and would let you use standard buttons on your sheet.这将使工作无需使用功能区即可工作,并且可以让您使用工作表上的标准按钮。

ok, stand back and cover your eyes :)好的,退后遮住你的眼睛:)

Sub Button1_Click()
    ' this short cut invokes the
    ' MyTestCommand function
    SendKeys "^Q"
End Sub

endure, i mean enjoy忍受,我的意思是享受

Try this:试试这个:

Sub Button1_OnClick()
    Application.Run("MyTestCommand")
End Sub

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM