Savas Adar
Savas Adar

Reputation: 4262

how to get selection range from excel 2010?

i cant get selection range from excel. i am using below code block and i can get active sheet like this. but i need only selection range. how can i do this?

Microsoft.Office.Interop.Excel.Application ExApp = Globals.ThisAddIn.Application as Microsoft.Office.Interop.Excel.Application;
Microsoft.Office.Interop.Excel.Worksheet ExWorksheet = ExApp.ActiveSheet as Microsoft.Office.Interop.Excel.Worksheet;
Microsoft.Office.Interop.Excel.Range activeSheet = ExWorksheet.UsedRange as Microsoft.Office.Interop.Excel.Range;

thanks for advice.

Upvotes: 8

Views: 13660

Answers (1)

Francesco Baruchelli
Francesco Baruchelli

Reputation: 7468

The selection is a property of the Application, so you should use something like:

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

Just be careful that the object returned by Selection could be something different fromn a Range (e.g. it could be a Chart), so you should check for null values of SelectedRange.

Upvotes: 12

Related Questions