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

Free Support Forum - aspose.com

MailMerge.setRemoveEmptyParagraphs() does function if there are more than one field per line

If there are more than one mailmerge field per line which results are empty or a blank, the line is not removed.

Another problem: if there is a blank before or behind the field, the line is not removed, too.

Kind regards,
Jens Boeckel
ABACUS Research AG
Switzerland

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

Thanks for your request. Only paragraphs that contain one empty mergefield will be removed. If you need to remove paragraphs during mail merge you can try using MergeField event handler. Please see the following link to learn more:

http://www.aspose.com/documentation/file-format-components/aspose.words-for-.net-and-java/the-mergefield-event.html

Also see the following code example.

//Open document

Document doc = new Document("C:\\Temp\\in.doc");

// Add MergeFiled event handler

doc.getMailMerge().addMergeFieldEventHandler(new HandleMergeFieldRemoveEmptyLine());

//Execute mail merge

String[] names = { "field1", "field2", "field3", "field4", "field5", "field6" };

Object[] values = { "value1", "", "value3", null, "value5", "" };

doc.getMailMerge().execute(names, values);

//save document

doc.save("C:\\Temp\\out.doc");

==================

class HandleMergeFieldRemoveEmptyLine implements MergeFieldEventHandler

{

public void mergeField(Object sender, MergeFieldEventArgs e) throws Exception

{

//Remove paragraph if value of mergefield is null or empty string

if(e.getFieldValue() == null || e.getFieldValue() == "")

{

CompositeNode parent = (CompositeNode)e.getField().getEnd().getAncestor(NodeType.PARAGRAPH);

if(parent!=null)

{

//If there is no more fields in the paragraph

if(parent.getChildNodes(NodeType.FIELD_START, true).getCount()==1)

parent.remove();

}

}

}

}

Hope this helps.

Best regards.

Thanks a lot, the code helped. But a problem was that in a mailmerge run no node could be removed. So I collected all matching nodes and removed them after the mailmerge run.


If blank should be removed, following code does it in the eventhandler:

Node nextSibling = e.getField().getEnd().getNextSibling();
if (nextSibling != null && isEmptyText(nextSibling.toTxt())) {
nextSibling.remove();
}