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

Free Support Forum - aspose.com

MailMerge.ExecuteWithRegions doesn't behave in the same way in version 8.1.0.0 and in latest Aspose.Words

Dear Support,


When upgrading my Aspose.Words dll (from 8.1.0.0 to 14.5.0.0), I realised that the “Aspose.Words.document.MailMerge.ExecuteWithRegions()” method doesn’t behave the same way as before. Could-you please indicate how I could go back to the initial situation with the latest Apsose.Words version?

I attached a sample project showing the problem.

Thanks,

Andy

Hi Andy,

Thanks for your inquiry. In your case, I suggest you please pass DataTable or DataSet to MailMerge.ExecuteWithRegions method. Please check the overloaded method of MailMerge.ExecuteWithRegions from here:
http://www.aspose.com/docs/display/wordsnet/ExecuteWithRegions+Method

Please read following documentation link for your kind reference.
http://www.aspose.com/docs/display/wordsnet/How+to++Execute+Mail+Merge+with+Regions

Please check the mail merge with regions code example in Aspose.Words for .NET examples repository at GitHub and let us know if you have any more queries.

Hi Tahir,


I have also tried with the overload “public void ExecuteWithRegions (DataTable dataTable)” and it gives the same result (see the attached code with the 2 aspose dll versions). The merge field is not replaced by an empty value with the latest version and it is with version 8.1.0.0. For my application I would need to keep the behavior of version 8.1.0.0. Do you have any suggestion/solution?

Thanks,

Andy,

Hi Andy,

Thanks
for your inquiry. If you want to remove the unused fields, empty
paragraphs, unused regions etc during mail merge process, please use the
MailMerge.CleanupOptions property. Following code snippet shows how to
instruct the mail merge engine to remove any empty paragraphs and unused regions.

doc.MailMerge.CleanupOptions
|= Aspose.Words.Reporting.MailMergeCleanupOptions.RemoveEmptyParagraphs;

doc.MailMerge.CleanupOptions |= Aspose.Words.Reporting.MailMergeCleanupOptions.RemoveUnusedRegions;

//doc.MailMerge.CleanupOptions |= Aspose.Words.Reporting.MailMergeCleanupOptions.RemoveUnusedFields;

//doc.MailMerge.CleanupOptions |= Aspose.Words.Reporting.MailMergeCleanupOptions.RemoveContainingFields;

doc.MailMerge.ExecuteWithRegions(resultantDataSet);

doc.Save(MyDir + @"Out.doc");


I suggest you please read following documentation links for your kind reference.
http://www.aspose.com/docs/display/wordsnet/How+to++Remove+Unmerged+Fields+and+Empty+Paragraphs+during+Mail+Merge
http://www.aspose.com/docs/display/wordsnet/MailMergeCleanupOptions+Enumeration

Moreover, you can also delete certain fields from the document by implementing IFieldMergingCallback interface as shown in following code example. Please check the following code example for your kind reference.

Hope this helps you. Please let us know if you have any more queries.

var doc = new Aspose.Words.Document(MyDir + "in.docx");

doc.MailMerge.FieldMergingCallback = new HandleMergeTest31();

doc.MailMerge.ExecuteWithRegions(dataSet);

doc.Save(MyDir + "Out.docx");

private class HandleMergeTest31 : IFieldMergingCallback

{

void IFieldMergingCallback.FieldMerging(FieldMergingArgs e)

{

if (e.TableName == "TableName" && e.DocumentFieldName.Equals("FieldName"))

{

if (e.FieldValue.ToString().Trim() == "")

e.Field.Remove();

}

}

void IFieldMergingCallback.ImageFieldMerging(ImageFieldMergingArgs e)

{

// Do nothing.

}

}


Thank you very much!

The option Aspose.Words.Reporting.MailMergeCleanupOptions.RemoveUnusedRegions solved my problem.

Andy

Hi Andy,

Thanks
for your feedback. Please feel free to ask if you have any question about Aspose.Words, we will be happy to help you.