Free Support Forum - aspose.com

Mail Merge Regions in IF content not working

Hello.
My company has Aspose.Words .NET 9.1.0.0, Developer OEM Subscription.
We wanted to have a Mail Merge Region that would be shown conditionally using IF field.
Aspose ExecuteWithRegions currently skips filling such regions.

Delving a bit deeper on the problem shows that MailMerge.Execute in turn sees the TableStart:TableName and the contained merge fields as normal merge fields. The correct behaviour is that MailMerge.Execute should ignore these.

Hello

<?xml:namespace prefix = o ns = "urn:schemas-microsoft-com:office:office" />

Thanks for your inquiry. Could you please provide me your template document and simple code which you use for fill the document with data; those will allow me to reproduce the problem on my side.

Best regards,

It turns out that MailMerge.ExecuteWithRegions is fine, only MailMerge.Execute is not.
In our scenario, we have a custom IMailMergeDataSource that returns placeholder values for unknown fields. Because we were first calling Execute, and later ExecuteWithRegions, the former broke the merge fields that were supposed to be interpreted only by the latter.

The following code sample exposes the problem in MailMerge.Execute when ran against the attached docx document:



/// NOTE: If this bug doesn’t exist, it is no longer relevant in which order we call MailMerge.Execute and MailMerge.ExecuteWithRegions
[Test]
[ExpectedException(typeof(EmptyFieldsMailMergeDataSource.AskedForFieldException))]
public void Bug_With_MailMerge_Execute_Doesnt_Skip_Merge_Regions_In_Ifs_Content_Exists()
{
var myTemplate = CommonEmbeddedFile.test_tables_in_ifs_docx;

var doc = new Document(myTemplate.GetStream());
doc.MailMerge.Execute(new EmptyFieldsMailMergeDataSource());
}


public class EmptyFieldsMailMergeDataSource : IMailMergeDataSource
{
public class AskedForFieldException : Exception { public AskedForFieldException() : base(“This IMailMergeDataSource was created for a document that does not contain any non-MergeRegion merge fields so it should not be asked for any!”) { } }

public bool GetValue(string fieldName, out object fieldValue)
{
throw new AskedForFieldException();
}

public bool MoveNext()
{
return true;
}

public string TableName
{
get { return null; }
}

public IMailMergeDataSource GetChildDataSource(string tableName)
{
// This is irrelevant for the test case
throw new NotImplementedException();
}


}


Hello

<?xml:namespace prefix = o ns = "urn:schemas-microsoft-com:office:office" />

Thank you for additional information. I cannot reproduce the problem on my side using the latest version of Aspose.Words and the following code:

// Create data table

DataTable table = new DataTable("Table1");

table.Columns.Add("Fld1");

table.Columns.Add("Fld2");

table.Columns.Add("Fld3");

table.Columns.Add("Fld4");

table.Columns.Add("Fld5");

// Add some dummy data.

table.Rows.Add(new object[] { "0", "1", "2", "3", "4"});

table.Rows.Add(new object[] { "0", "1", "2", "3", "4"});

table.Rows.Add(new object[] { "0", "1", "2", "3", "4"});

// Open document

Document doc = new Document("test_tables_in_ifs.docx");

doc.MailMerge.Execute(new string[] { "myField1" }, new string[] {"test" });

doc.MailMerge.ExecuteWithRegions(table);

doc.UpdateFields();

doc.Save("out.docx");

Best regards,

This should help you understand what I meant in the previous posts:


// Create data table

DataTable table = new DataTable(“Table1”);
// For the demonstration in this very example to work, it seems you need to call ExecuteWithRegions with a DataSet and not a DataTable, otherwise it will fill just one table in the document
DataSet dataSet = new DataSet();
dataSet.Tables.Add(table);

table.Columns.Add(“Fld1”);

table.Columns.Add(“Fld2”);

table.Columns.Add(“Fld3”);

table.Columns.Add(“Fld4”);

table.Columns.Add(“Fld5”);


// Add some dummy data.

table.Rows.Add(new object[] { “0”, “1”, “2”, “3”, “4” });

table.Rows.Add(new object[] { “0”, “1”, “2”, “3”, “4” });

table.Rows.Add(new object[] { “0”, “1”, “2”, “3”, “4” });



// Open document

Document doc = new Document(CommonEmbeddedFile.test_tables_in_ifs_docx.GetStream());



// We supply empty values for region fields. They should be ignored, but because of the bug, they aren’t ignored in the body of IF clause.
// This is equivalent to our custom IMailMergeDataSource returning placeholder values for all fields it does not have values for.
// Quoting from http://www.aspose.com/documentation/.net-components/aspose.words-for-.net/howto-execute-simple-mail-merge.html
// “Note that a simple mail merge done using MailMerge.Execute ignores fields that are inside mail merge regions. Only merge fields that are not inside any mail merge region are populated.”

doc.MailMerge.Execute(new string[] { “Table1”, “Fld1”, “Fld2”, “Fld3”, “Fld4”, “Fld5” }, new string[] { “”, “”, “”, “”, “”, “” });


// You must execute with DataSet to fill all the tables in the document

doc.MailMerge.ExecuteWithRegions(dataSet);



doc.UpdateFields();



doc.Save(“out.docx”);

Hello

<?xml:namespace prefix = o ns = "urn:schemas-microsoft-com:office:office" />

Thank you for additional information. I managed to reproduce the problem on my side. Your request has been linked to the appropriate issue. You will be notified as soon as it is resolved.

Best regards,

The issues you have found earlier (filed as 18335) have been fixed in this update.


This message was posted using Notification2Forum from Downloads module by aspose.notifier.