c# - 如何从办公室功能区控件中获取选定的单元格

标签 c# vsto excel-2010

我想从功能区控件中获取事件范围。只有当功能区上的控件需要时,才应该有一种方法可以访问选定的单元格。

目前我就是这样做的;

public partial class ThisAddin
{
    private void SheetSelectionChange(object sh, Range target)
    {
        int count = target.Count;

            if (count < 5000) // This is for performance reasons 
            {
                //Set a custom range property in the office ribbon
                Req_Tool.ActiveRange = target; 
            {
    }
}

我觉得这是软弱和浪费。
首先,每次选择更改时,我的代码都会运行。 其次,无论是否使用过,我都有两份选集。

必须有更好的方法来做到这一点,我忽略了。

最佳答案

您可以通过访问 Application 对象的 Selection 属性来访问事件工作表中当前选定的范围。

private void button1_Click(object sender, RibbonControlEventArgs e)
{
    this.ActiveRange = (Excel.Range)Globals.ThisAddIn.Application.Selection;
}

MSDN 文档:

关于c# - 如何从办公室功能区控件中获取选定的单元格,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11909091/

相关文章:

c# - 使用 Moq,我如何验证一个对象的所有属性都被复制了?

c# - 如何将图表从 Excel 文件复制到 PowerPoint?

c# - ASP.NET Core Controller 中的模拟 HttpRequest

c# - 使 Visual Studio 调试器以正确的应用程序为目标

c#-4.0 - 在 VSTO(Word 应用程序)中获取调用被被调用者异常拒绝

c# - 在运行时向 RibbonDropDown 添加项目

excel - 当多个单元格都为真时如何填充单元格

vba - Excel VBA 函数仅在更改时更新

c# - 遍历模型属性并在代码文件中获取值

excel - 从数据透视表引用数据的公式