【问题标题】:How to get selected cells from an office ribbon control如何从办公室功能区控件中获取选定的单元格
【发布时间】:2012-08-10 20:53:35
【问题描述】:

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

目前我就是这样做的;

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; 
            {
    }
}

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

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

【问题讨论】:

    标签: c# vsto excel-2010


    【解决方案1】:

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

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

    MSDN 文档:

    【讨论】:

    • 以前这只会为我选择集合中的第一个单元格。我意识到在将类型明确指定为范围时。您必须将其设为 List selectedCells 或 var。我认为这是值得说明的。当然,现在这似乎很明显...... Go Stack!
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2017-09-18
    • 2013-08-24
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多