zzjove
zzjove

Reputation: 195

How to get the active excel range by using VSTO?

I want to get the selected or focused range in Excel. better returns me a Range object. I knew activeCell, but it only gives me one cell. What I need are a range of cells. Hope you can get my question. Thanks.

Upvotes: 0

Views: 4164

Answers (1)

JFish222
JFish222

Reputation: 1026

Credit to @Tim as he got here first and gave good start for the answer (give the man an up tick!).

But just to add to it (in 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)
  2. "ThisAddIn" should be replaced with your Add-In name.
  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.

Upvotes: 3

Related Questions