Remove Merge Field from document

Hi,
I am having a document template and after resolving the Merge Fields, I get a document.
Even after the Mail Merge, the FILL-IN and ASK merge fields will be present in the notice.
If you click (Ctrl+F9), these fields will become visible in the output document.

I want to remove these merge fields from the o/p document.
I got a link for doing that from:
Remove merge fields from output documents

It works fine with ASK field as the value chosen by the user will be present on the bookmark only.
But that is not the case with FILL-IN field, in which case the chosen value is embedded with in the FILL-IN field.
I want the value chosen by the user to be present in the place where FILL-IN field was present.

I am attaching a sample merged document along with.
Please let me on how do I go about this issue.

Thanks

Hi

Thanks for your inquiry. FILL-IN and ASK are not merge fields. Please try using the following code to replace fields in your document with text:

// Open source document
Document doc = new Document(@"Test043\in.docx");
// unlink fields in the document
UnlinkFields(doc);
// Save outut document
doc.Save(@"Test043\out.docx");
private void UnlinkFields(Document doc)
{
    // Get collection of FieldStart nodes
    NodeCollection fieldStarts = doc.GetChildNodes(NodeType.FieldStart, true);
    // Get collection of FieldSeparator nodes
    NodeCollection fieldSeparators = doc.GetChildNodes(NodeType.FieldSeparator, true);
    // And get collection of FieldEnd nodes
    NodeCollection fieldEnds = doc.GetChildNodes(NodeType.FieldEnd, true);
    // Loop through all FieldStart nodes
    foreach(FieldStart start in fieldStarts)
    {
        // Search for FieldSeparator node. it is needed to remove field code from the document
        Node curNode = start;
        while (curNode.NodeType != NodeType.FieldSeparator && curNode.NodeType != NodeType.FieldEnd)
        {
            curNode = curNode.NextPreOrder(doc);
            if (curNode == null)
                break;
        }
        // Remove all nodes between Fieldstart and FieldSeparator (of FieldEnd, depending from field type)
        if (curNode != null)
        {
            RemoveSequence(start, curNode);
        }
    }
    // Now we can remove FieldStart, FieldSeparator and FieldEnd nodes
    fieldStarts.Clear();
    fieldSeparators.Clear();
    fieldEnds.Clear();
}
/// 
/// Remove all nodes between start and end nodes, except start and end nodes
///
/// The start node
/// The end node
public void RemoveSequence(Node start, Node end)
{
    Node curNode = start.NextPreOrder(start.Document);
    while (curNode != null && !curNode.Equals(end))
    {
        // Move to next node
        Node nextNode = curNode.NextPreOrder(start.Document);
        // Check whether current contains end node
        if (curNode.IsComposite)
        {
            if (!(curNode as CompositeNode).GetChildNodes(NodeType.Any, true).Contains(end) &&
                !(curNode as CompositeNode).GetChildNodes(NodeType.Any, true).Contains(start))
            {
                nextNode = curNode.NextSibling;
                curNode.Remove();
            }
        }
        else
        {
            curNode.Remove();
        }
        curNode = nextNode;
    }
}

Hope this helps.
Best regards.

It works great.!
Thanks