Free Support Forum - aspose.com

Mergefield output in Microsoft Word not consistent

Hello there,

After setting the mergefield in Microsoft Word to be bold and font size 12, I ran my Java code with Aspose Words. However, only the first two words are in bold and font size 12 while the rest were not.
Eg. mergefield in word: <>
value from java: “This goes into myFieldName”
Only “This” and “goes” were in bold and font size 12
“into” and “myFieldName” were not in bold and font size 14 instead

For example in Word, i have Insert > Quick Parts > Field > MergeField > myFieldName > Preserve formatting during updates > OK. I then keep the font as Times New Roman, bold the myFieldName and set it to font size 12. After saving the Word document, i closed it.

Inside Java, i have the following as example:

String myFieldValue = “This goes into myFieldName”;
Document doc = new Document(reportDir + templateFilename);
doc.getMailMerge().execute(new String[] {“myFieldName”}, new Object[] {myFieldValue});
doc.save(reportDir + filename + “.docx”);

Please advise. I have created a Microsoft Word template with several mergefields created. As such the advice given in applying custom formatting is not very efficient as alot of mergefields were affected.

Using the following:
Eclipse Java EE IDE for Web Developers (Photon Release 4.8.0)
Microsoft Office Word 2007
Java Version 8 Update 101 (build 1.8.0_101-b13)

there is a missing word in line 3 inside the <>
the word is myFieldName

@timtam,

Thanks for your inquiry. To ensure a timely and accurate response, please ZIP and attach the following resources here for testing:

  • Your simplified input Word document
  • Aspose.Words 18.9 generated output DOCX file showing the undesired behavior
  • Your expected DOCX document showing the correct output. You can create expected document by using Microsoft Word.

As soon as you get these pieces of information ready, we will start further investigation into your issue and provide you more information. Thanks for your cooperation.

MailMerge.zip (131.2 KB)

Hello,

Attached the zipped file as requested.

  • MailMerge Template: Your simplified input Word document
  • MailMerge Out: Aspose.Words 18.9 generated output DOCX file showing the undesired behavior
  • MailMerge Expected: Your expected DOCX document showing the correct output. You can create expected document by using Microsoft Word.

Kindly note the font size, bold, underline differences in the template and the output document. Thanks!

@timtam,

You can use either of the following workarounds:

Document doc = new Document("D:\\MailMerge\\MailMerge Template.docx");

for (Field field : (Iterable<Field>) doc.getRange().getFields()) {
    if (field.getType() == FieldType.FIELD_MERGE_FIELD) {

        FieldMergeField mf = (FieldMergeField) field;
        mf.getFormat().getGeneralFormats().remove(GeneralFormat.MERGE_FORMAT);
    }
}

doc.getMailMerge().execute(new String[] {"Company"}, new Object[] {"Company name goes here"});

doc.save("D:\\MailMerge\\awjava-18.9.docx"); 

Or

Document doc = new Document("D:\\MailMerge\\MailMerge Template.docx");

for (Field field : (Iterable<Field>) doc.getRange().getFields()) {
    if (field.getType() == FieldType.FIELD_MERGE_FIELD) {
        Node currentNode = field.getStart();

        boolean isContinue = true;
        while (currentNode != null && isContinue) {
            if (currentNode.getNodeType() == NodeType.FIELD_END)
                isContinue = false;

            if (currentNode.getNodeType() == NodeType.RUN) {
                Run run = (Run) currentNode;
                run.getFont().setName("Cambria");
                run.getFont().setSize(26);
                run.getFont().setBold(true);
            }

            Node nextNode = currentNode.nextPreOrder(currentNode.getDocument());
            currentNode = nextNode;
        }
    }
}

doc.getMailMerge().execute(new String[]{"Company"}, new Object[]{"Company name goes here"});

doc.save("D:\\MailMerge\\awjava-18.9.docx");

Hi Awais Hafeez,

Thanks for the quick response! However, that is not what i need.

I need some merge fields to be in bold, while some with underline. There are also different parts of the document which requires different font sizes. Is it possible to provide me a workaround with more merge fields examples (besides “Company”) so that i can understand how each merge field is formatted?

With respect to the same sample documents i have sent you, i need

*fullname: Calibri (Body), 11
*Company: Cambria(Headings), 26, Bold
*ADDRESS: Calibri (Body), 8, Bold, Capitalized
*City: Calibri (Body), 12, Bold, Underlined

In my actual document, i have more merge fields than these… Is there a way where it detects the format of each merge field in the Microsoft Word template and produces the word(s) in that particular format without me having to fix/dictate the formats beforehand?

Thanks so much!

@timtam,

Please try using the following code to get the desired output:

Document doc = new Document("D:\\MailMerge\\MailMerge Template.docx");

for (Field field : (Iterable<Field>) doc.getRange().getFields()) {
    if (field.getType() == FieldType.FIELD_MERGE_FIELD) {

        Run targetRun = null;
        if (field.getSeparator().getNextSibling().getNodeType() == NodeType.RUN) {
            targetRun = (Run) field.getSeparator().getNextSibling();
        }

        Node currentNode = field.getStart();
        boolean isContinue = true;

        while (currentNode != null && isContinue) {
            if (currentNode.getNodeType() == NodeType.FIELD_END)
                isContinue = false;

            if (currentNode.getNodeType() == NodeType.RUN) {
                if (targetRun != null) {
                    Run run = (Run) currentNode;
                    run.getFont().setName(targetRun.getFont().getName());
                    run.getFont().setSize(targetRun.getFont().getSize());
                    run.getFont().setBold(targetRun.getFont().getBold());
                    run.getFont().setUnderline(targetRun.getFont().getUnderline());
                }
            }

            Node nextNode = currentNode.nextPreOrder(currentNode.getDocument());
            currentNode = nextNode;
        }
    }
}

doc.getMailMerge().execute(new String[]{"FullName", "Company", "Address", "City"},
        new Object[]{"FullName goes here", "Company name goes here", "Address goes here", "City name goes here"});

doc.save("D:\\MailMerge\\awjava-18.9.docx");

Thank you for your reply awais! It works perfectly!

Just checking, why did you use
“Node nextNode = currentNode.nextPreOrder(currentNode.getDocument());”
instead of
“Node nextNode = currentNode.getNextSibling();”?

@timtam,

Please refer to the following API reference pages: