Code suggestion to remove ASK and FILLIN fields?

Hello,

Would you be so kind as to give me a little code snippet on how I could loop through a Word document and remove all of the “ASK” fields? I’m sure that would help with “FILLIN” fields too, right?

I’m trying to merge to a set of templates that may have these and I need to get rid of them!

Thanks for your suggestion!
Mike

Hello Mike!
Thank you for your inquiry.
Fields in Aspose.Words document model consist of several sequential nodes from FieldStart to FieldEnd including them. There is no one-call API to remove field of any kind. If you are sure that the fields subjected for removal fit in one paragraph each and don’t contain nested fields then you can try this simple function:

private static void RemoveSomeFields()
{
    Document doc = new Document("source.doc");
    // Collect all FieldStart nodes. We'll find and remove some nodes at these points.
    NodeCollection startNodes = doc.GetChildNodes(NodeType.FieldStart, true);
    foreach(FieldStart start in startNodes)
    {
        if ((start.FieldType == FieldType.FieldAsk) || (start.FieldType == FieldType.FieldFillIn))
        {
            Node curNode = start;
            while (curNode != null)
            {
                // We should first get the next node then remove this one.
                // This is because Remove() breaks the chain.
                Node nextNode = curNode.NextSibling;
                curNode.Remove();
                if (curNode.NodeType == NodeType.FieldEnd)
                    break;
                curNode = nextNode;
            }
        }
    }
    doc.Save("result.doc");
}

Regards,

Thank you, Viktor! That was just what I was looking for.

Mike