Exception in NameCollection.RemoveAt()

Hello,

I’ve got InvalidOperationException: “Collection was modified; enumeration operation may not execute.” when tried to remove names for the attached workbook.

Here is code which you can use to test:

NameCollection names = workbook.Worksheets.Names;
int i = names.Count - 1;
while (i >= 0)
{
names.RemoveAt(i);
i–;
}

Is it correct to use the next workaround:
1) Save in separate collection those names which I want to keep alive
2) Call workbook.Worksheets.Names.Clear()
3) Add saved in step #1 names to cleared collection

Can this workaround break some internal relationships, or it is safe?

Many thanks,
Andrey


Hi,

Please download and try: Aspose.Cells for .NET v7.0.4.6

I have tested your scenario using the following code with your template file and it works fine. I can successfully generate the output file having null named ranges in it, all the named ranges are deleted fine.


Sample code:
Aspose.Cells.Workbook workbook = new Aspose.Cells.Workbook(“e:\test2\Aspose_NameRemove.xlsm”);



NameCollection names = workbook.Worksheets.Names;
int i = names.Count - 1;
while (i >= 0)
{
names.RemoveAt(i);
i–;
}

workbook.Save(“e:\test2\output.xlsm”, SaveFormat.Xlsm);


Hello,


Thank you for the reply.
Forgot to mention - I used v. 7.0.4.3.

I’ve checked v. 7.0.4.6 - it works correctly.

Many thanks,
Andrey

Hi,

We recommend you to kindly try latest version v7.1.0 :
http://www.aspose.com/community/files/51/.net-components/aspose.cells-for-.net/entry355234.aspx