[Question] MailMerge.ExecuteWithRegions - Multipy "TableStart" "TableEnd"


#1

Hello Aspose Team,

On my Word docuemnt, there are 2 places that I put down "TableStart" & "TableEnd", the interesting part is, what if they both pointing to the same dataset?

Example in Word:

«TableStart:DatasetA» «field1» «field2» «TableEnd:DatasetA»

Some text, some text

«TableStart:DatasetA» «field1» «field2» «TableEnd:DatasetA»

My testing result show the second "TableStart" and "TableEnd" will not populate with any data and the first one is fine.

Is this a bug or is it by design?

My idea is, the second one should be populated as well becuase they are valid code and there are times when the same data need to repeat on the document.

Thanks in advance for your help and hope to hear from you soon.


#2

That is by design. If you want to fill repeating regions with the same data you can use the following code:

while (DocumentHasMergeField(doc, "TableStart:DatasetA"))

{

doc.MailMerge.ExecuteWithRegions(datasetA);

}

This code uses the following helper method:

///

/// Check if the specified document contains merge field with the specified name.

///

/// Document to check.

/// Merge field name.

/// True, if the specified document contains merge field with the specified name. Otherwise, false.

private bool DocumentHasMergeField(Document doc, string mergeFieldName)

{

foreach (string name in doc.MailMerge.GetFieldNames())

{

if (name == mergeFieldName)

return true;

}

return false;

}

Best regards,


#3

Thanks and I will try that.


#4

Tested, works perfectly, thanks!