Free Support Forum -

MergeField getName() Truncated at 40 Characters


I have pored over the other issues with MergeFields, and have noted a couple of the issues have had to do with the 40 character truncation, but haven’t found a solution that works for me yet.

We need to update the names of all of our MergeFields, and I would like to use the getName() function as detailed in the example from the documentation, but that returns only the first 40 characters. Without being able to pull more than the first 40 characters, I cannot know for sure how to formulate the new name for the MergeField, making the automation of this task impossible.

I think I may have to use the DOM (in a fashion shown in the documentation) and traverse all nodes to find MergeFields. Is this advisable, or are there issues with this approach of which I should be aware?

Basically, what is the BEST PRACTICES way of getting the full names of the MergeFields in order to change those MergeField names before saving off a copy of the document that is structurally sound and not lacking any information contained in the original document?

Thank you very much for any help,

Looks like I can get all the full names by doing something like this:

String[] fieldNames = thisDoc.getMailMerge().getFieldNames()
fieldNames.each { thisOne ->
  println thisOne

But I want to know if there isn’t some way to get the full names with the getName() method, or if I will have to brute force update all the full names and do the same thing with the short names by replacing them with a substr(x,x) kind of magic.

Thanks again for any help,

Hi Andy,

Sorry, I was unable to reproduce this issue using the latest version of Aspose.Words for Java. I tried getting the names with more than 80 characters at my end and all names were extracted successfully using doc.getMailMerge.getFieldNames().

Please confirm if you are using the latest version and you have also applied a valid license.

Best Regards,

That’s right. I can get the full name that way, but not with the getName() method as demonstrated in the example for updating the mergefield names, which is what I am needing to do.

So if I were to get all the full names using the doc.getMailMerge.getNames() method and put those in a list, then run through the document again and this time using the getName() on each mergefield, I could update the name using setName() and pass the new value – the full value – and that would work? Would the updating of the short version (40 characters long) work automatically, or would I have to do something to update that, as well?

Thanks for the reply,

Hi Andy,

We are working on this issue and will update you soon.

Best Regards,

Hi Andy,

Sorry for the delay.

Yes, you are right, that code returns the first 40 characters of field names. You can update your code to get complete names e.g. you can use the following code to get complete names of merge fields.

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

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

// Select all field start nodes so we can find the merge fields.

NodeCollection fieldStarts = doc.getChildNodes(

NodeType.FIELD_START, true);

for (FieldStart fieldStart : (Iterable) fieldStarts) {

if (fieldStart.getFieldType() == FieldType.FIELD_MERGE_FIELD) {

String fieldCode = getFieldCode(fieldStart);

int startPos = fieldCode.indexOf("MERGEFIELD ");

startPos = (startPos >= 0) ? startPos + 1 : 0;

int endPos = fieldCode.indexOf(" \\* MERGEFORMAT");

endPos = (endPos >= 0) ? endPos : fieldCode.length();

String fieldName = fieldCode.substring(startPos, endPos);




Best Regards,

Thanks very much.

Hi again,

So I’m assuming I need to import com.aspose.words.Field

And then I am kind of fuzzy on how I declare a field object. The code above does not work as-is… can’t set getFieldCode on a fieldstart, apparently.

Thanks for your continued assistance,

Hi Andy,

Following was the getFieldCode method I used at my end.

private static String getFieldCode(FieldStart fieldStart) {

StringBuilder builder = new StringBuilder();

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

for (Node node = fieldStart; node != null

&& node.getNodeType() != NodeType.FIELD_SEPARATOR

&& node.getNodeType() != NodeType.FIELD_END; node = node

.nextPreOrder(node.getDocument())) {

// Use text only of Run nodes to avoid duplication.

if (node.getNodeType() == NodeType.RUN)



return builder.toString();


You need to import following packages.

import com.aspose.words.Document;

import com.aspose.words.FieldStart;

import com.aspose.words.FieldType;

import com.aspose.words.Node;

import com.aspose.words.NodeCollection;

import com.aspose.words.NodeType;

Best Regards,