Unplanned
Last Updated: 24 Mar 2020 15:30 by ADMIN
Alexander
Created on: 03 Mar 2020 11:10
Category: WordsProcessing
Type: Bug Report
1
MailMerge fails if the document contains a "table of contents" with at least 2 entries

I have created a very simple template (see attached file), import it, add my contents and later try to do a MailMerge.
However, the call to this function fails with the following exception:

System.ArgumentException
  HResult=0x80070057
  Message=The document element is already associated with a parent.
Parametername: item
  Source=Telerik.Windows.Documents.Flow
  StackTrace:
   at Telerik.Windows.Documents.Flow.Model.Collections.DocumentElementCollection`2.VerifyDocumentElementOnInsert(T item) in c:\DeveloperTooling_Agent13\_work\91\s\Documents\Flow\Flow\Model\Collections\DocumentElementCollection.cs:line 69
   at Telerik.Windows.Documents.Core.Data.DocumentElementCollectionBase`2.InsertRange(Int32 index, IEnumerable`1 items) in c:\DeveloperTooling_Agent13\_work\91\s\Documents\Core\Core\Core\Data\DocumentElementCollectionBase.cs:line 129
   at Telerik.Windows.Documents.Flow.Model.InlineRangeEditor.InsertInlinesInRange(InlineBase start, IEnumerable`1 inlines) in c:\DeveloperTooling_Agent13\_work\91\s\Documents\Flow\Flow\Model\InlineRangeEditor.cs:line 132
   at Telerik.Windows.Documents.Flow.Model.Fields.FieldInfo.UpdateFieldCore(FieldUpdateContext context) in c:\DeveloperTooling_Agent13\_work\91\s\Documents\Flow\Flow\Model\Fields\FieldInfo.cs:line 236
   at Telerik.Windows.Documents.Flow.Model.Fields.FieldInfo.UpdateFieldInternal(FieldUpdateContext context) in c:\DeveloperTooling_Agent13\_work\91\s\Documents\Flow\Flow\Model\Fields\FieldInfo.cs:line 186
   at Telerik.Windows.Documents.Flow.Model.MailMergeProcessor.ExecuteMailMerge(RadFlowDocument document, Object record) in c:\DeveloperTooling_Agent13\_work\91\s\Documents\Flow\Flow\Model\MailMergeProcessor.cs:line 57
   at Telerik.Windows.Documents.Flow.Model.MailMergeProcessor.Execute(RadFlowDocument document, IEnumerable collection) in c:\DeveloperTooling_Agent13\_work\91\s\Documents\Flow\Flow\Model\MailMergeProcessor.cs:line 25
   at Telerik.Windows.Documents.Flow.Model.RadFlowDocument.MailMerge(IEnumerable collection) in c:\DeveloperTooling_Agent13\_work\91\s\Documents\Flow\Flow\Model\RadFlowDocument.cs:line 337
   at JOIM.TextExport.DocumentGenerator.Export(String outputPath) in P:\Tolaris\JOIM.Common\JOIM.TextExport\DocumentGenerator.cs:line 581


using Telerik.Windows.Documents.Flow.FormatProviders.Docx;
using Telerik.Windows.Documents.Flow.Model;
using Telerik.Windows.Documents.Spreadsheet.Model;

...

using (StreamfileStream = File.Open(templatePath, FileMode.Open, FileAccess.Read, FileShare.ReadWrite))
{
    var provider = newDocxFormatProvider(); 
    _document = provider.Import(fileStream);
}

...

_document = Document.MailMerge(new [] { MergeFieldData });

Attached Files:
2 comments
ADMIN
Dimitar
Posted on: 04 Mar 2020 09:03

Hi Alexander,

The TOC field is not currently supported in the RadWordsProcessing library. We have a feature request for this in our feedback portal. You can track its progress, subscribe to status changes and add your comment to it here: WordsProcessing: Table Of Contents - TOC field.

In this case, the mail merge functionality tries to update the filed and fails because the merge filed is not supported. I have approved this issue as well and it will remain as a separate item. To work around it you can set the IsLocked property of the fields before performing the mail merge: 

var fields = _document.EnumerateChildrenOfType<FieldCharacter>();

foreach (var item in fields)
{
    if (item.FieldInfo.Field is MergeField)
    {
        continue;
    }
    item.FieldInfo.IsLocked = true;

}
var merged = _document.MailMerge(mailMergeDataSource);

fields = merged.EnumerateChildrenOfType<FieldCharacter>();

foreach (var item in fields)
{
    if (item.FieldInfo.Field is MergeField)
    {
        continue;
    }
    item.FieldInfo.IsLocked = false;

}

Should you have any other questions do not hesitate to ask.

Regards,
Dimitar
Progress Telerik

Get quickly onboarded and successful with your Telerik and/or Kendo UI products with the Virtual Classroom free technical training, available to all active customers. Learn More.
Alexander
Posted on: 03 Mar 2020 13:38

I have debugged this error and found that you check in VerifyDocumentElementOnInsert that the items to insert do not have any parent. However, when removing a whole paragraph (via DeleteContentDifferentParagraphs), the parents from the Inlines are not removed.

The problem, however, is that the contents of the TOC are deleted, which is not what one expects...