Reputation: 2616
I am trying to get only used range within excel worksheet , but I am getting error that says
Object reference not set to an instance of an object.
Microsoft.Office.Interop.Excel.Worksheet xlWSheet = null;
Excel.Range xlCell = xlWSheet.UsedRange.SpecialCells(Excel.XlCellType.xlCellTypeLastCell,Type.Missing);
Excel.Range oRng = xlWSheet.get_Range("A1", xlCell);
Upvotes: 0
Views: 680
Reputation: 4164
Its because you invoking member on xlWSheet which is set to null
Microsoft.Office.Interop.Excel.Worksheet xlWSheet = null;
For example : you should have proper assignment before using it.
var app = new Application { Visible = true };
app.Workbooks.Add();
Microsoft.Office.Interop.Excel.Worksheet xlWSheet = app.ActiveSheet
Upvotes: 0
Reputation: 6020
You need to create an instance of Microsoft.Office.Interop.Excel.Worksheet.
Microsoft.Office.Interop.Excel.Worksheet xlWSheet = new Microsoft.Office.Interop.Excel.Worksheet();
Otherwise, your object is null and thus you are getting NullReferanceException.
Upvotes: 2