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

Free Support Forum - aspose.com

How to get all unmerged fields?

Hi,

as per the title, is there a way using Aspose.Words for Java to get all those merge fields that, after one or more merge operation, are still unmerged?

Hi Matteo,


Thanks for your inquiry. You can get a collection of mail merge field names available in the document by using the MailMerge.GetFieldNames method. You can also iterate through the remaining merge fields collection by using the following code:
for(Field field : doc.getRange().getFields())
{
if (field.getType() == FieldType.FIELD_MERGE_FIELD){
//do something
}
}
I hope, this helps.

Best regards,

Hi,

it seems your code snipped works correctly, thank you.

However, given merge field f, how do I get its name?

I tried f.getFieldCode(), but it would return all of the field code, and I would have to extract the name from it, is there a way to get directly only the name?

Hi Matteo,


Thanks for your inquiry. You can extract merge field name from the field code by using the following code snippet:
for(Field field : doc.getRange().getFields())
{
if (field.getType() == FieldType.FIELD_MERGE_FIELD){
Pattern regex = Pattern.compile("\s*(MERGEFIELD\s|)(\s|)(\S+)\s+");
Matcher matcher = regex.matcher(field.getFieldCode());
matcher.find();
String name = matcher.group(3).toString();
}
}
I hope, this helps.

Best regards,