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

Free Support Forum - aspose.com

MailMerge IF condition extra CR/LF

Hi,
I am running v9.4.0.0 and performing an ExecuteWithRegions on a document with many ‘IF’ conditions.
Attached are the datasource, template, and merged document. I have mergefields within IF conditions and one of the conditional fields is called ‘DELETE’ to demonstrate the issue. If you look at the merged document and look for the text ‘DELETE’ (which I set the text of the MERGEFIELD in my mergeevent), you will see that it is followed by a carriage return/line feed.
Under the table that starts with ‘Reference B/75/1340/BA’ you will see two lines, DELETE and ‘blank line’. I actually don’t want any blank lines here but having just the one will be manageable.
Is there a way for me to remove one of these lines. I have tried using document.Range.Replace but this doesn’t seem to remove \r\n - am I doing something wrong or should I be iterating through DocumentBuilder nodes? If so please could you provide example code for me.
This is quite an urgent problem so would very much appreciate a quick response.
Thank you in advance
Regards,
Floyd

Hello

Thanks for your request. Could you please also attach your code here for testing? I will check the problem on my side and provide you more information.
Best regards,

procedure void DoMerge
{
    Document docTemplate = new Document(msTemplate);
    docTemplate.MailMerge.RemoveEmptyParagraphs = true; 
    ProcessMergeFields pmf = new ProcessMergeFields();
    docTemplate.MailMerge.FieldMergingCallback = pmf;
    string[] fieldNames = docTemplate.MailMerge.GetFieldNames(); 
    for (int i = 0; i<fieldNames.Length; i++)
    {
        if (fieldNames[i].Contains("TableStart:"))
        {
            docTemplate.MailMerge.ExecuteWithRegions(CriteriaData);
            break;
        }
    }
    foreach (DataTable dt in CriteriaData.Tables)
    {
        if (!dt.Columns[0].ColumnName.Contains(":"))
        {
            docTemplate.MailMerge.Execute(dt);
        }
    }
    // Delete any leftover fields
    docTemplate.MailMerge.DeleteFields();
}
private class ProcessMergeFields : IFieldMergingCallback
{
    void IFieldMergingCallback.FieldMerging(FieldMergingArgs e)
    {
        if (e.FieldName == "DELETE")
        {
            e.Text = "DELETE";
            return;
        }
        // Cope with null 
        if (e.FieldValue == null)
        {
            e.Text = "";
            return;
        }
    }
}

Hi
Thank you for additional information. I think, the problem is in your template but not in your code.
I think, the extra paragraph break occurs because you have a paragraph break after IF field with DELETE merge field and paragraph break before table in the IF field followed DELETE field. That is why you see an extra paragraph break. You can try refactoring your template to resolve the problem.
Best regards,

When I remove the paragraph break a compilation error occurs when executing ‘doc.MailMerge.ExecuteWithRegions(DataSet)’
The error is:

StackTrace " at System.Collections.Stack.Pop()\r\n at xfbd1009a0cbb9842.xe25d778fe9f1252a.VisitFieldEnd(FieldEnd fieldEnd)\r\n at Aspose.Words.Fields.FieldEnd.Accept(DocumentVisitor visitor)\r\n at Aspose.Words.CompositeNode.x464d2134480a7bf2(DocumentVisitor x672ff13faf031f3d)\r\n at Aspose.Words.CompositeNode.xf7ae36cd24e0b11c(DocumentVisitor x672ff13faf031f3d)\r\n at Aspose.Words.Paragraph.Accept(DocumentVisitor visitor)\r\n at xfbd1009a0cbb9842.xe25d778fe9f1252a.xf098323036d9ec26(Node xda5bf54deb817e37)\r\n at xfbd1009a0cbb9842.xe25d778fe9f1252a.x105bab38d511372f(ArrayList xa955664f4f50999d, Boolean x0d900d42b3598fde)\r\n at xe86f37adaccef1c3.xc5c3f438428cb03b.xd01ea9131ba53b87(ArrayList x292b5f49fee42032)\r\n at xe86f37adaccef1c3.xc5c3f438428cb03b.xdeeb682062ef79a5()\r\n at xe86f37adaccef1c3.xc5c3f438428cb03b.xd5da23b762ce52a2(MailMerge x2a99de81295e7f29, IMailMergeDataSource xef1769c4fe6ae4ca, Boolean xc9c7b90943167aed)\r\n at xe86f37adaccef1c3.xc5c3f438428cb03b.xd01ea9131ba53b87(ArrayList x292b5f49fee42032)\r\n at xe86f37adaccef1c3.xc5c3f438428cb03b.xdeeb682062ef79a5()\r\n at xe86f37adaccef1c3.xc5c3f438428cb03b.xd5da23b762ce52a2(MailMerge x2a99de81295e7f29, IMailMergeDataSource xef1769c4fe6ae4ca, Boolean xc9c7b90943167aed)\r\n at Aspose.Words.Reporting.MailMerge.ExecuteWithRegions(IMailMergeDataSourceRoot dataSourceRoot)\r\n at Aspose.Words.Reporting.MailMerge.ExecuteWithRegions(DataSet dataSet)

Attached is the revised template.
Thank you
Regards,
Floyd

Hi
Thank you for additional information. Now the problem occurs because you have region start and DELETE condition in the same paragraph. For each region whole paragraph, where region start is located, is repeated. That why you get broken fields in the output document and that is why exception occurs.
Maybe you can try just narrowing down font size of the paragraph with regions start.
Best regards,

Reducing the size of the document has helped with the formatting so thank you for the advice. It has of course also made it difficult to design & maintain.
If I understand you correctly the reason this error occurs is because you cannot have repeated regions based on a TableStart. Is this by design and is it something you will change in a future release?
Thanks very much for your help
Regards,
Floyd

Hi
Thanks for your inquiry. This is by design. Region cannot occupy only part of a Paragraph. If paragraph contains region, whole paragraph will be repeated.
Best regards,