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

Free Support Forum - aspose.com

Inserting a table with mailmerge

is it possible to insert a table created inn docx-format to a mailmerge field? Or is there a another way to insert tables with mailmerge?

@ron.inputdata.no,

Please try using the following code. Hope, this helps.

Document doc = new Document("D:\\Temp\\in.docx");
Document subDoc = new Document("D:\\Temp\\input.docx"); // this .docx contains only the Table

doc.getMailMerge().setFieldMergingCallback(new HandleMergeField());
doc.getMailMerge().execute(new String[] { "mf" }, new Object[] { subDoc });

doc.save("D:\\Temp\\awjava-19.1.docx");
////////////////////////////////////////////////////////////////
private static class HandleMergeField implements IFieldMergingCallback {
    public void fieldMerging(FieldMergingArgs args) throws Exception {
        if (args.getFieldName().equals("mf")) {
            DocumentBuilder builder = new DocumentBuilder(args.getDocument());
            builder.moveToMergeField(args.getFieldName());

            builder.insertDocument((Document) args.getFieldValue(), ImportFormatMode.USE_DESTINATION_STYLES);
        }
    }

    public void imageFieldMerging(ImageFieldMergingArgs args) {

    }
}