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

Joe*_*ton 3 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; 
            {
    }
}
Run Code Online (Sandbox Code Playgroud)

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

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

Pet*_*eed 6

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

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

MSDN文档: