We're sorry Aspose doesn't work properply without JavaScript enabled.

Free Support Forum - aspose.com

Iteration variable NumberOf returns incorrect index

Weird bug in the LINQ reporting engine: If a document expression (with -build enabled) is used within the scope of a foreach tag, it can access the iteration variable, but it doesn’t get updated when it loops. For example:

Main document:

<<foreach [item in Data.List]>>
<<doc [InsertedDocument] -build>>

Inserted document:


We would expect to see output like:


But the actual output is:



To ensure a timely and accurate response, please attach the following resources here for testing:

  • Your input Word document.
  • Please create a standalone console application ( source code without compilation errors ) that helps us to reproduce your problem on our end and attach it here for testing.

As soon as you get these pieces of information ready, we will start investigation into your issue and provide you more information. Thanks for your cooperation.

PS: To attach these resources, please zip and upload them.

sample console app.zip (19.2 KB)
Hi @tahir.manzoor ,

I have attached the zipped console app with the input documents and source code (source code is in Program.cs file). It will produce the output document in the same folder. It’s worth noting that the bug only occurs when the subdocument is provided to the reporting engine as an Aspose Document object in memory - if it is provided as a file or a byte array, output is as expected. If the subdocument is provided as a stream, the reporting engine throws a “Cannot access a closed file” exception.



We have tested the scenario and have managed to reproduce the same issue at our side. For the sake of correction, we have logged this problem in our issue tracking system as WORDSNET-19676. You will be notified via this forum thread once this issue is resolved.

We apologize for your inconvenience.

@tahir.manzoor Thanks for the quick response!


It is to inform you that the issue which you are facing is actually not a bug in Aspose.Words. So, we have closed this issue (WORDSNET-19676) as ‘Not a Bug’.

Please read the detail from here:
Inserting Documents Dynamically

  • Using of a single document instance on iteration

The same document instance cannot be used to fill it with data multiple times. Once a document instance is filled with data, it becomes a ready report and it cannot be used as a template second time. So in your scenario, the subdocument is filled with data on the first iteration and then it is not refilled on the next ones, hence the same data appears several times in a result document.

To make it work as expected, a unique document instance should be provided to the engine on every iteration. We have modified your code and template to get the desired output. Pleas check the attachment. DocsModified.zip (19.5 KB)

Thanks @tahir.manzoor for your quick response. We’ll use the workaround you’ve provided.