Free Support Forum -

Error clearing headers

When I execute the following code on the attached document, I get an error stating:

Operation is not valid due to the current state of the object. at Aspose.Word.?.MoveNext()

What is this and how do I avoid it?


Dim Doc As Aspose.Word.Document = New Aspose.Word.Document(FileName)
Dim builder As DocumentBuilder = New DocumentBuilder(Doc)
For Each Sec In builder.Document.Sections


Thank you for your interest in Aspose.Word.

This is the enumerator failure. It detects that the collection it's enumerating has been changed therefore throws. Avoid use of foreach loop, replace it with simple for:

for (int i = 0; i < doc.Sections.Count; i++)


Section section = doc.SectionsIdea [I];




By the way, I have some notes regarding your code:

1) What is it supposed to do? Remove all headers/footers from the document? If so, why do you move to the primary footer first?

2) Use of DeleteHeaderFooterShapes is redundant as you clear whole content from the header/footer then.