Free Support Forum - aspose.com

Object reference not set to an instance of an object when copying worksheet

When I try to copy the "report" sheet from the attached template to a new workbook, I receive the following error.

System.NullReferenceException: Object reference not set to an instance of an object.
at Aspose.Cells.ಐ.ন(ಐ ঩)
at Aspose.Cells.Cell.ు(Cell ঩)
at Aspose.Cells.Cells.ন(Cells ঩)
at Aspose.Cells.Worksheet.ন(Worksheet ୿, Boolean ஁)
at Aspose.Cells.Worksheet.Copy(Worksheet sourceSheet)
at WindowsApplication2.Form1.Form1_Load(Object sender, EventArgs e) in C:\Documents and Settings\jrozier\My Documents\Visual Studio 2005\Projects\WindowsApplication2\WindowsApplication2\Form1.vb:line 18

Code:

Dim x As New Aspose.Cells.Workbook
Dim y As New Aspose.Cells.Workbook

x.Open("C:\test\pRPT92.xls")

y.Worksheets.Add("NewSheet")
y.Worksheets("NewSheet").Copy(x.Worksheets("report"))
y.Save("C:\test\prpt921.xls")

This happens in 4.4.1.0, but not 4.4.0.21.

Actually, the first file posted fails in both versions, but the file I’m posting now only fails in 4.4.1.0

Hi,

Thanks for the template file,

After an initial test, we found the problem. We will figure it out soon.

Thank you.

Is there something about the template that I might be able to change to get it to work in the meantime?

Hi,

Well, currently there is a problem with Copy method, so you should not use it for the time being till it is resolved. I think you may try: open your template workbook (having your required sheets), save it as to create a new one.

Thank you.

Unfortunately, I can't use that work-around. I need multiple copies of the sheet being copied and I need VBA in the resulting report that I'm pulling from another template. Worksheet.Copy works with some of my templates. I've attached one. Is there something specific about the templates that caused the failure that I can modify? I'm supposed to deploy to production this Friday and need something soon.

Thanks,
Josh

Hi Josh,

Thanks for your patience. Sorry for our delay.

Please try this fix.

In this fix,we use Cell.GetStyle and SetStyle method replace the property of Cell.Style. The two methods will save more memory usage than cell.Style property. The feature caused that we could not response quickly.

If you want to set style of the cell, please change your codes as :

//Getting the style of the cell.

Style style = cell.GetStyle();

//Setting the property of the style.

.......

//Setting the style of the cell

cell.SetStyle(style);

Warren,

This fixes the issue stated above, but the Cells.Rows property is missing and the Row type is missing. Also, Worksheets.GetNamedRanges fails with an Index out of range error.

Thanks,
Josh Rozier

Hi Josh,

Thanks for considering Aspose.

We did change some internal structure of the Aspose.Cells component related formattings. We have to sacrifice this for some optimization and performance measures and did eliminate / modify some APIs which we have now in more enhanced shape. You may utilize some newer APIs of Cells class instead e.g.., ApplyRowStyle() etc. We will soon publish an article for the users to list the deprecated apis and the newer ones for the replacement.

For Worksheets.GetNamedRanges() method, we don't find the problem as I tested, Could you give us some sample code with template file to re-produce the issue. We will check it soon.

Thank you.

Amjad,

The article about the APIs would be great.

I'm attaching the file that fails when calling the GetNamedRanges() method.

Here's the code:

Dim x As New Aspose.Cells.Workbook

x.Open("C:\test\EBITDASummaryTemplate.xls")

Dim arRg() As Range
arRg = x.Worksheets.GetNamedRanges

Thanks,
Josh

Hi Josh,

Thanks for the template file.

After an initial test, we did find an issue retrieving named ranges in your template file. We will further check to figure it out soon.

Thanks for being patient!