Edit several Mergefields with the same text

54 Views Asked by At

I am using MSDN example code to replace a mergefield with some text. However if I have more than one mergefield with the same text, the code only affects one and not the others. How do I solve this?

This is the code I'm using

foreach (var field in mainPart.Document.Descendants<SimpleField>()){
    string fieldText = GetFieldName(field);
    if (fieldText.Equals(fldName))
    {
        string strText = null;
        foreach (Text objText in field.Descendants<Text>())
        {
            strText = objText.OuterXml;
            objText.Remove();
            break;
        }
        field.Parent.Append(new Run(new Text("Hello")));
        mainPart.Document.Save();
    }
}

I have 3 simplefields but it only replaces one.

0

There are 0 best solutions below