Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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.

like image 531
Savas Adar Avatar asked May 18 '12 11:05

Savas Adar


People also ask

How do I select the range of columns in Excel?

To select a range of cells, click on the first cell in the range and then drag your mouse over the rest of the cells you want to select. Alternatively, you can click on the first cell in the range and then hold down the Shift key while clicking on the last cell in the range. 2.

How do I change the selection range in Excel?

When selecting a small range that consists of just a few cells, click the first cell and drag to the last cell you want included in the range. To select a larger range, it's easier to click the first cell and hold down the Shift key while you click the last cell in the range.


1 Answers

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.

like image 54
Francesco Baruchelli Avatar answered Oct 11 '22 09:10

Francesco Baruchelli