如何使用ExcelDna中的ExcelCommand

问题描述 投票:2回答:2

我有一个简单的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";
}

当我按下Ctrl + Shift + Q时,第一张表格中的单元格A1会收到文本"foo bar"

客户端不需要快捷方式,他想要一个用户界面按钮(在功能区或工作表中,无关紧要)。

使用VBA我可以写:

Sub Button1_OnClick()
    MyTestCommand
End Sub

但是调用MyTestCommand不起作用。

如何调用我创建的命令?

c# excel vba excel-addins excel-dna
2个回答
2
投票

如果为MenuTextExcelCommand成员设置一个值,它将自动通过Add-Ins功能区提供。

enter image description here

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

如果您想要更好地控制命令的呈现方式,那么使用您自己的按钮,图标等创建自己的CustomUI .Take a look at the different Ribbon examples that come with Excel-DNA


1
投票

通过使用VBA古怪的SendKeys功能,您可以使用非常可怕的frig。这样就可以在不使用功能区的情况下工作,并且可以让您在工作表上使用标准按钮。

好的,站起来遮住你的眼睛:)

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

忍受,我的意思是享受

© www.soinside.com 2019 - 2024. All rights reserved.