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

Free Support Forum - aspose.com

Continue Numbering fails on MailMerge

I have a strange issue to report. The list feature “Continue Numbering” is failing whenever Document.MailMerge.Execute is called after appending documents that contain numbered lists.
I’m using Aspose.Words v18.11.0.

Here are the steps to reproduce:

Create and save 2 new Word documents (I have samples, but I don’t see where to attach them to this topic). Both documents should contain a simple numbered list like this:

Numbered List Part 1.docx

1.	First Line
2.	Second Line
3.	Third Line

Numbered List Part 2.docx

1.	Fourth Line
2.	Fifth Line
3.	Sixth Line

Code to Reproduce

Document doc = new Document("Numbered List Part 1.docx");
Document doc2 = new Document("Numbered List Part 2.docx");
doc2.FirstSection.PageSetup.SectionStart = SectionStart.Continuous;
doc.AppendDocument(doc2, ImportFormatMode.UseDestinationStyles);

List<string> fieldNames = new List<string>();
List<object> fieldValues = new List<object>();
doc.MailMerge.Execute(fieldNames.ToArray(), fieldValues.ToArray());

doc.Save("C:\\test.docx", Aspose.Words.SaveFormat.Docx);

Expected Output

1.	First Line
2.	Second Line
3.	Third Line
4.	Fourth Line
5.	Fifth Line
6.	Sixth Line

Actual Output

1.	First Line
2.	Second Line
3.	Third Line
1.	Fourth Line
2.	Fifth Line
3.	Sixth Line

Here’s the Really Strange Part

If you remove the call to Document.MailMerge.Execute (line #8), the output is generated as expected.
The data source lists are empty in this example, but the same behavior exhibits when the lists are populated.
Also, the same behavior exhibits when calling Document.ExecuteWithRegions.

@Bill_Hanson,

Please see these input/output Word documents: test-documents.zip (39.7 KB)

We were unable to reproduce the said behavior with Aspose.Words for .NET 18.11 when executing the following code on our end:

Document doc = new Document("D:\\Temp\\Numbered List Part 1.docx");
Document doc2 = new Document("D:\\Temp\\Numbered List Part 2.docx");

doc2.FirstSection.PageSetup.SectionStart = SectionStart.Continuous;
doc.AppendDocument(doc2, ImportFormatMode.UseDestinationStyles);

List<string> fieldNames = new List<string>();
List<object> fieldValues = new List<object>();
doc.MailMerge.Execute(fieldNames.ToArray(), fieldValues.ToArray());  // commenting this does not have any effect

doc.Save("D:\\Temp\\18.11-Execute.docx");

Similar outputs were produced on our end in both cases.

Hi Awais,

Thank you for taking the time to reply.

The input files that you used in your tests do not exhibit the behavior that I am describing. In fact, neither of your outputs shows the expected results, which is continued numbering 1 through 6. Both of your outputs show 1 through 3, then 1 through 3 again.

I’ve attached the input files I used in my tests so that you can also see this behavior. I hope this helps.

Numbered Lists.zip (20.5 KB)

@Bill_Hanson,

We tested the scenario and managed to reproduce the same problem on our end. For the sake of correction, we have logged this problem in our issue tracking system. The ID of this issue is WORDSNET-17738. We will further look into the details of this problem and will keep you updated on the status of correction. We apologize for your inconvenience.

@Bill_Hanson,

Regarding WORDSNET-17738, it is to update you that we have completed the analysis of this issue and come to a conclusion that we would not be able to implement the fix to this issue. Your issue (WORDSNET-17738) has now been closed as ‘Not a Bug’ resolution. Please see the following analysis details:

MS Word and Aspose.Words apply restartNumberingAfterBreak attribute for each list in document during mail merge. You may use the List.IsRestartAtEachSection property to reset the restartNumberingAfterBreak attribute after mail merge:

foreach (List list in doc.Lists)
	list.IsRestartAtEachSection = false;

Hope, this helps.

This response is not acceptable. By your response, it is clear that you do not understand the issue. The workaround that you provided is the exact opposite of what I am trying to show you.

Also, this BUG is not present when using NodeImporter.ImportNode / Body.AppendChild, only when using Document.AppendDocument. I have a workaround in place, but assumed you would want to improve your product.

How can you accept that the output is one way with merge fields and another way without? Explain that please.

@Bill_Hanson,

We are looking into this issue further and will get back to you with more details soon.

@Bill_Hanson,

Regarding WORDSNET-17738, unfortunately, this is still neither a bug nor an issue in Aspose.Words. This is how Microsoft Word works.

The list numbering restart attribute was added in WORDSNET-15054 and was mentioned in Aspose.Words for .NET 17.9 Release Notes (see Added Public Property List.IsRestartAtEachSection section). This section includes the generic workaround for backward compatibility during mail merge:

The following is a more generic solution and hopefully, it will satisfy your needs:

Dictionary<List, bool> lists = new Dictionary<List, bool>();
 
foreach (List list in document.Lists)
    lists[list] = list.IsRestartAtEachSection;
 
document.MailMerge.Execute(...);
 
foreach (KeyValuePair<List, bool> pair in lists)
    pair.Key.IsRestartAtEachSection = pair.Value

Please find Attachments.zip (19.2 KB) i.e. False Out.docx, True Out.docx. True Out.docx is the one generated with the workaround applied. The numbering in there is 1 2 3 4 5 6. I am afraid, we do not understand how it can be opposite.

Again, this is not a bug but expected behavior implemented to mimic MS Word. NodeImporter.ImportNode and Body.AppendChild do not create new sections while Document.AppendDocument does, after applying mail merge the list containing a section break gets its numbering reset.

The answer is as mentioned above. MS Word and Aspose.Words apply restartNumberingAfterBreak attribute for each list in document during mail merge.

P.S. Please also note that we are in a constant process of getting closer and closer to MS Word - the product whose behavior we try to mimic. That is why from time to time we have to change the behavior. Sometimes the changes are breaking, but we cannot introduce a new API property to support each change. However, we do mention these changes in release notes section. Thank you for your understanding.

Thank you for following up on this issue.

Regarding the release notes, yes I read about the new list property, but the change in behavior of MailMerge.Execute is not documented well. The release notes do not mention that list.IsRestartAtEachSection is set to TRUE for all lists when MailMerge.Execute is called, it just has a vague reference to ‘backward compatibility upon Mail Merge’ with no description about what is actually changing.

Thanks again for your help. The workaround from the release notes is what I needed.

@Bill_Hanson,

Thanks for your suggestions and feedback. It is great that the workaround is acceptable for you.

We will also be sure to improve online documentation in this regard. We apologize for any inconvenience.