Free Support Forum - aspose.com

Invalid Region No Longer Generates Error

In previous versions, an error would be generated if a region name (TableStart:MyRegion) didn't exist in the dataset being passed in for the ExecuteWithRegions. Now, however, it seems that no error is generated and the document simply gets rendered with the invalid region as plain text.

<>Data<>

<>More Data<>

<>

DataSet has the following tables:

Region1

Region2

Is there a way to prevent the document from rendering and display a message to the user that the template is invalid?

Thank you

Hello

Thanks for your request. I think you should just specify RemoveEmptyRegions property. In this case empty mail merge regions will be removed from the document.

Please see the following link to learn more:

http://www.aspose.com/documentation/.net-components/aspose.words-for-.net/aspose.words.reporting.mailmerge.removeemptyregions.html

Please let me know if this way is acceptable for you.

Best regards,

The field to remove empty regions (RemoveEmptyRegions) is already set to true. I have also tried to set the RemoveEmptyParagraphs to true as well. Neither of these have either removed the empty region nor thrown an error indicating that the template is malformed. We just currently upgraded our libraries to use the latest Words dll (10.1.0.0).

If you could provide another possible solution, it would be greatly appreciated.

Thank you.

Hi Terry,

Thanks for your inquiry.

The RemoveEmptyRegions propety will only remove a region if the table exists in the data source (but has no rows). It will be left untouched if the table doesn't exist.

In your case you can use the code below to find any left over regions in the document. The code should be executed whafter all mail merge has been executed.

string[] fieldNames = doc.MailMerge.GetFieldNames();<?xml:namespace prefix = o ns = "urn:schemas-microsoft-com:office:office" />

foreach (string name in fieldNames)

{

if (name.Contains("TableStart"))

{

// Remove the start tag plus the separator character.

string regionName = name.Remove(0, 11);

Console.WriteLine("The region: {0} was unmerged", regionName);

}

}

Also curiously, I don't think any version of Aspose.Words would throw an exception if a region was missing from the data source, instead the region would just be ignored. Could you please inform us with what verson you were using?

If you have any further queries, please feel free to ask.

Thanks,

Thanks Adam,

It turns out that prior to this latest upgrade we were using 7.0.0.0. It also turns out that an error was not generated just as you say, but the extraneous TableStart was removed during the call to ExecuteWithRegions. Prior to this upgrade, we had not used code similar to what you provided.

Thank you very much for the code snippet. I will let you know if we continue to experience issues.

Hi

Thank you for additional information. In 7.0.0 Aspose.Words automatically removes empty regions. In later versions we changed the behavior and added an option that indicates that empty regions should be removed.

Best regards,