Metadata on mail merge field

I’m using Aspose Words Java.

I need to associate metadata (some string) with each mail merge field in a document, such that the post-mailmerge substituted text still has the metadata available.

I was mostly able to get this to work by attaching a bookmark to each mail merge field, where the name of each bookmark is the metadata I want to persist. After the mail merge, the substituted text is still located inside of the bookmark. So I can extract out the metadata by looking at the name of each bookmark.

However, as a part of the mail merge certain portions of the document are duplicated (we use a library called Sablon which supports for loops in Word documents), which causes there to be duplicate bookmark with the same ID.

When there are bookmarks with duplicate IDs doc.getRange().getBookmarks() no longer works properly. It considers all data between the start of the first copy of the duplicated bookmark and the end of the last copy of the duplicated bookmark to be part of one bookmark.

Not sure how to proceed. I also tried using editable ranges, but those have the same problem with duplicate IDs. All I really need is someway of wrapping the mail merge field in another node, and setting an attribute on this parent node.

@veered,

Please see these sample input/output Word documents (Docs.zip (18.2 KB)) and try running the following code:

Document doc = new Document("D:\\temp\\input.docx");
DocumentBuilder builder = new DocumentBuilder(doc);

int i = 1;
for (Field field : (Iterable<Field>) doc.getRange().getFields()) {
    if (field.getType() == FieldType.FIELD_MERGE_FIELD) {
        FieldMergeField mf = (FieldMergeField) field;
        String fieldName = mf.getFieldName();

        // Let our Meta Data merge field names start with _
        if (!fieldName.startsWith("_")) {
            builder.moveToMergeField(fieldName, true, false);
            builder.getFont().clearFormatting();

            // Bookmarks with duplicate names are not allowed
            // So, instead of Bookmarks, lets insert another merge field next to original merge field
            // Lock this new merge field, so that it does not get updated during Mail Merge
            FieldMergeField newMf = (FieldMergeField) builder.insertField(FieldType.FIELD_MERGE_FIELD, false);
            newMf.setFieldName("_" + fieldName);
            newMf.setResult("SomeValue_" + i++);
            newMf.isLocked(true);
        }
    }
}

// Mail Merge will not operate on Locked merge fields
doc.getMailMerge().execute(new String[]{"mf1", "mf2", "_mf1", "_mf2"},
                           new Object[]{" Hello ", " World ", "1", "2"});

doc.save("D:\\Temp\\awjava-18.6.docx");

Thanks for your response, I like this approach. I think I need to place an extra mergefield on either side of the normal mergefields, so I can tell where the substituted text both begins and ends.

I’ve gotten that part working; now how do I extract out the text between the two merge fields?

I ended up doing this (in jruby), where field is a merge field:

    sibling = field.getEnd().getNextSibling()
    if sibling
      text = sibling.text
      sibling.setText('')

      doc_builder.moveToField(field, true)
      doc_builder.insertHyperlink(text, "http://bogus/?#{field_name}", false);
    end

    field.remove()

@veered,

It is great you were able to find what you were looking for. Please let us know any time you have any further queries.

Also, please see the following article:
Extract Selected Content Between Nodes