Error when reading worksheet from template

Feb 17, 2011 at 8:19 AM
Edited Feb 17, 2011 at 8:29 AM

Hi All,

                 I am trying to read a xlsx template in a loop for some data processing. I am able to load the the template some times and process data and save the new excel(xlsx) with a file name. How ever some times the statement below throws up a object reference not set to an instance error as shown below :

Statement throwing the error:

Dim ExcelWSSummary As ExcelWorksheet = pckTemplate.Workbook.Worksheets(1)

Error Description:

Object reference not set to an instance

Stack Trace Information :

at OfficeOpenXml.ExcelRangeBase..ctor(ExcelWorksheet xlWorksheet, String address)
   at OfficeOpenXml.ExcelWorkbook.GetDefinedNames()
   at OfficeOpenXml.ExcelPackage.get_Workbook()

HResult - -2147467261


Code snippet:

For i= 5 To 10

            Dim objNewFile As New FileInfo("c:\test" & i.ToString() & ".xlsx")
            Dim objTemplate As New FileInfo("c:\Template.xlsx")
            Dim pckTemplate As New ExcelPackage(objNewFile, objTemplate)

            Dim ExcelWSSummary As ExcelWorksheet = pckTemplate.Workbook.Worksheets(1)   -  (This is where the error occurs )

            'Do some processing here

           'Save the file through pckTemplate.Save()

Next i

 Appreciate any inputs on the same.



Feb 17, 2011 at 6:30 PM


Version 2.8 can't read templates with Defined names that reference a Value, formula or an external cell address.

I have added a fix for this in the latest source --> tags/v2.8.0.1 (it will remove names it can't handle).

Next version will be able to handle this kind of names.


Sep 30, 2011 at 11:05 AM

Hi Jan,

Please let me know when you get a fix for this issue, I have made a separate post on the same issue at :

I am using the latest build, and the Named Range are randomly stripped off values (eg. I have 2012 Naed range, but only 1876 are populated in Names).

Some of the fields that go missing are of type dropdown (using DataValidation List). and some have default value set (example Company Name field reads (Enter Company Name))


Please Suggest.