Replace fill-in field by mergefield

Hi,

Let me start by situating my problem :

I have a lot
of old word templates that used to be populated with data using Word’s
mail merge function. Some of these documents contain Fill-In fields.
Merging these documents the old way, Word kept popping up a textbox,
asking to fill in the required fields. (As it should be doing)

Since
I want to use these templates with Aspose.Words, I need some way for
the functionality of these Fill-in fields to remain. Since all the
templates have to be registered in our system, I thought it would be
easy to find and replace any Fill-in fields at the time they (the
templates) are registered and stored.

Example: I have a template containing 3 Fill-in fields.

  • Fill-In Shoe size?
  • Fill-In Lenght?
  • Fill-In Weight?

When I register this template in our new system, I need the Fill-in fields to be replaced by Mergefields.

  • MERGEFIELD FI_SHOESIZE
  • MERGEFIELD FI_LENGTH
  • MERGEFIELD FI_WEIGHT

I then store the templates and some metadata.
This metadata being :

  • FI_SHOESIZE = Shoe size?
  • FI_LENGTH=Length?
  • FI_WEIGHT=Weight?

Now I can pass the data needed along together with my request to perform a mail-merge.

My question:
How can I achieve this using Aspose.Words? Is there an easy way to change the FieldType?

Thanks in advance

Regards

Matthias

Hi
Thanks for your request. Unfortunately, there is no easy way to change type of fields. To achieve this, you should find your FILLIN fields, remove them and insert MERGEFIELDS instead. I created a simple code that demonstrated the technique:

// Open document
Document doc = new Document("C:\\Temp\\in.doc");
// Create DocuentBuilder. It will help us to modify fields
DocumentBuilder builder = new DocumentBuilder(doc);
// Get collection of FieldStart nodes
Node[] starts = doc.getChildNodes(NodeType.FIELD_START, true).toArray();
// loop through all field starts and search for FILLIN fields
for (int i = 0; i < starts.length; i++)
{
    FieldStart start = (FieldStart)starts[i];
    if (start.getFieldType() == FieldType.FIELD_FILL_IN)
    {
        // Every field in MS Word document consists of FieldStart, FieldSeparator, FieldEnd nodes
        // And Runs that represent field code and field value (displayed text)
        // If you need to remove the field you should remove all these nodes
        Node currentNode = start;
        // Get Field code
        while (currentNode.getNodeType() != NodeType.FIELD_END)
        {
            Node nextNode = currentNode.getNextSibling();
            // Remove current and move to the next node.
            currentNode.remove();
            currentNode = nextNode;
        }
        // Insert new field, but first move DocumentBuider cursor to the end of the field.
        String name = "MergeFieldName";
        builder.moveTo(currentNode);
        builder.insertField(String.format("MERGEFIELD %s", name), String.format("«%s»", name));
    }
}
// save output document
doc.save("C:\\Temp\\out.doc");

Hope this helps.
Best regards,

Hi

Thanks a lot, exactly what I needed!

Regards

Matthias

1 Like