简体   繁体   English

如何通过使用VSTO获得有效的Excel范围?

[英]How to get the active excel range by using VSTO?

I want to get the selected or focused range in Excel. 我想在Excel中获得所选范围或重点范围。 better returns me a Range object. 更好地返回我一个Range对象。 I knew activeCell, but it only gives me one cell. 我知道activeCell,但是它只给我一个单元。 What I need are a range of cells. 我需要的是一系列单元格。 Hope you can get my question. 希望你能回答我的问题。 Thanks. 谢谢。

Credit to @Tim as he got here first and gave good start for the answer (give the man an up tick!). 感谢@Tim,因为他先来到这里,并为答案提供了良好的开端(给那个人一个勾号!)。

But just to add to it (in C#) . 但是只是添加到它(在C#中)。 . .

The follow code will provide global scope (Get you the desired info anywhere in your Add-In application). 以下代码将提供全局范围(在外接程序中的任何位置为您提供所需的信息)。

Microsoft.Office.Interop.Excel.Range stuff =
     Globals.ThisAddIn.Application.Selection as Excel.Range;

Notes: 笔记:

  1. Check that (stuff != null) 检查(东西!= null)
  2. "ThisAddIn" should be replaced with your Add-In name. “ ThisAddIn”应替换为您的加载项名称。
  3. Selection returns a value of type dynamic (it is completely ignored by the compiler). 选择返回一个动态类型的值(编译器将完全忽略它)。 As a result (and as far as the compiler is concerned), it has no type until it is cast/assigned to a variable. 结果(就编译器而言),在将其强制转换/分配给变量之前,它没有任何类型。

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

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