How to identify if a FieldIF contains a SET?


In some legacy VBA code I have some logic that loops through all the fields in a document and unlinks the IF statements.

HOWEVER, should the IF statement contains a SET, for example { SET var “Y” }, the unlink is skipped as the SET needs to remain to be used further down in the document.

The VBA code to check whether to skip the unlink is : InStr(1, oField.Code, "SET ")

I cannot figure out a way to replicate this behaviour in Aspose.Words as the TrueText and FalseText variables only contain the plain text, not the field code like VBA has.

For example, if my TrueText is “This is true{ SET var “N”}” in the Word document, the TrueText when stepping through my code in C# is just “This is true”. In VBA, the .Code result is “This is true [] SET var “N” []”, i.e. the word SET appears.

How do I know if an IF statement in the document contains field codes?

Is there Aspose.Words field code field which contains all the run text within the field?

There doesn’t seem to be any property which contains the rich run text within the IF field. I was expecting a Runs property against the TrueText and FalseText.

Below is a Word document demonstrating. I want the yellow highlighted text to be “it equals NO!”. (17.2 KB)



Thanks for your inquiry. In your case, we suggest you please unlink the IF field as shown below. Hope this helps you.

Document doc = new Document(MyDir + "TestSETREF.docx");
foreach (Field _field in doc.Range.Fields.Cast<Field>().ToList().ToArray())
    if (_field.Type == FieldType.FieldSet || _field.Type == FieldType.FieldRef)
        // do not unlink
    else if (_field.Type == FieldType.FieldIf && _field.GetFieldCode().Contains("Set"))
        UnlinkIfField(_field, doc);
doc.Save(MyDir + "18.1.docx");

private static void UnlinkIfField(Field field, Document document)
    Node currentNode = field.Start.NextPreOrder(document); 
    FieldEnd fieldend = field.End;

    while (currentNode != fieldend)
        if (currentNode.NodeType == NodeType.FieldStart)
            if (((FieldStart)currentNode).FieldType == FieldType.FieldSet)
                currentNode = ((FieldStart)currentNode).GetField().End;

        Node node = currentNode.NextPreOrder(document);
        currentNode = node;

    if (field.Start != null)

    if (fieldend != null)


Thanks Tahir, it was the

that I needed to get business logic to work.

Appreciate your response and time.



Thanks for your feedback. Please feel free to ask if you have any question about Aspose.Words, we will be happy to help you.