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

Free Support Forum - aspose.com

MaliMerge breaks numbering in some word documents

Hi Aspose team!
I’m using Aspose Total (21.11 for Words.NET) and sometimes executing simple MailMerge list numbering become broken. Below is an example of such a document.

DataSource:

    public class DocumentDataSource
    {
        public string RegDate { get; set; }

        public string No { get; set; }
    }

    public class DocumentMailMergeDataSource : IMailMergeDataSource
    {
        private bool _isRead;
        private readonly DocumentDataSource _source;

        public const string DocRegDateFieldName = "DOKREGDATUMS";
        public const string DocNoFieldName = "DOKREGNUMURS";

        public DocumentMailMergeDataSource(DocumentDataSource source)
        {
            _source = source;
        }

        public string TableName => null;

        public IMailMergeDataSource GetChildDataSource(string tableName)
        {
            return null;
        }

        public bool GetValue(string fieldName, out object fieldValue)
        {
            switch (fieldName)
            {
                case DocRegDateFieldName:
                    fieldValue = _source.RegDate;
                    return true;
                case DocNoFieldName:
                    fieldValue = _source.No;
                    return true;
                default:
                    fieldValue = null;
                    return false;
            }
        }

        public bool MoveNext()
        {
            if (_isRead)
            {
                return false;
            }

            _isRead = true;

            return _isRead;
        }
    }

Code used for MailMerge:

			var doc = new Words.Document(@"C:\\Docs\bad_doc.docx");

			var documentDataSource = new DocumentDataSource()
			{
				RegDate = "Some date",
				No = "Number"
			};

			doc.MailMerge.UseNonMergeFields = true;
			doc.MailMerge.PreserveUnusedTags = false;
			doc.MailMerge.CleanupOptions = MailMergeCleanupOptions.RemoveUnusedFields | MailMergeCleanupOptions.RemoveUnusedRegions;
			doc.MailMerge.Execute(new DocumentMailMergeDataSource(documentDataSource));

			doc.Save(@"C:\\Docs\afterMerge.docx");

bad_doc.docx (170.4 KB)

After MailMerge list numbering is:
1.Some text.
1.Some other text:
1.1. Bla-bla-bla;
1.2. Again bla-bla-bla.

@SANDIMUS Thank you for reporting this problem to us. I have managed to reproduce it and logged it as WORDSNET-22973 in our defect tracking system We will keep you informed and let you know once it is resolved.

@SANDIMUS The issue you have found earlier is not a bug. MS Word and Aspose.Words restart all lists in document during mail merge.
You should use the Document.MailMerge.RestartListsAtEachSection option to override this behavior.