Current filter:
                                You should refresh the page.
                                  • DevExpress supplies a code sample for replacing document fields with their values:
                                    https://www.devexpress.com/Support/Center/Example/Details/E3482/how-to-replace-document-fields-with-their-values

                                    But this sample works wrong if there are nested fieldf and we want to replace inner fields with their values.
                                    For example, we have a complex field { IF {PAGE} = "1" "First page" "Another page ({PAGE})" }. When we replace inner fields with values as described in the mentioned sample we get  { IF2{PAGE} = "1" "First page" "Another page (2{PAGE})" } for second page. The field becomes invalid or (for other formula types) leads to doubled string values.
                                    The modified sample code that demonstrates this behaviour is attached.

                                    Is there a correct way to replace inner fields?

                                • Sergey Anoshkin 01.01.2018

                                  Sorry, in the attachment above a little code fragment that selects inner fields only was commented out. 

                                1 Solution

                                Creation Date Importance Sort by

                                Hello Sergey,
                                In your scenario, I suggest updating the nested fields recursively as shown below:

                                [C#]
                                private void button1_Click(object sender, EventArgs e) { richEditControl1.Document.Fields.Update(); FieldsToValues(richEditControl1.Document, richEditControl1.Document.Range); } private void FieldsToValues(Document doc, DocumentRange range) { ReadOnlyFieldCollection rangeFields = doc.Fields.Get(range); foreach(Field currentField in rangeFields) { currentField.ShowCodes = true; FieldsToValues(doc, currentField.CodeRange); string fieldCode = doc.GetText(currentField.CodeRange); string fieldResult = doc.GetRtfText(currentField.ResultRange); DocumentPosition fieldPosition = currentField.Range.Start; doc.Delete(currentField.Range); doc.InsertRtfText(fieldPosition, fieldResult); } }

                                I have attached the modified version of your project to demonstrate this approach in action.
                                Please check it and let me know whether or not this solution meets your requirements.

                                • Sergey Anoshkin 01.09.2018

                                  As I need to replace inner fields only I tried this code:

                                  [C#]
                                  private void button1_Click(object sender, EventArgs e) { richEditControl1.Document.Fields.Update(); FieldsToValues(richEditControl1.Document, richEditControl1.Document.Range); } private void FieldsToValues(Document doc, DocumentRange range) { ReadOnlyFieldCollection rangeFields = doc.Fields.Get(range); foreach(Field currentField in rangeFields) { currentField.ShowCodes = true; FieldsToValues(doc, currentField.CodeRange); if (currentField.Parent == null) continue; string fieldCode = doc.GetText(currentField.CodeRange); string fieldResult = doc.GetRtfText(currentField.ResultRange); DocumentPosition fieldPosition = currentField.Range.Start; doc.Delete(currentField.Range); doc.InsertRtfText(fieldPosition, fieldResult); } }

                                  But the code works wrong. The result for page 2 is: { IF 22 = "1" "First page" "Another page (22)" }
                                  I debugged the code and found that th following. The first call of FieldsToValues operates with both inner and outer fields. Inner fields go first in rangeFields collection. Subcall of FieldsToValues() for each of them does nothing. Next lines

                                  [C#]
                                  string fieldCode = doc.GetText(currentField.CodeRange); string fieldResult = doc.GetRtfText(currentField.ResultRange); DocumentPosition fieldPosition = currentField.Range.Start; doc.Delete(currentField.Range); doc.InsertRtfText(fieldPosition, fieldResult);

                                  do not delete the field but do insert a value of the field just before its code.
                                  Outer fields go last in the rangeFields collection. Subcall of FieldsToValues() for each of them replaces fields to their values correctly.

                                  Eventually I went to the code below that does not contain direct replacements in outer FieldsToValues() call and works correctly:

                                  [C#]
                                  private void button1_Click(object sender, EventArgs e) { richEditControl1.Document.Fields.Update(); FieldsToValues(richEditControl1.Document); } private void FieldsToValues(Document doc) { ReadOnlyFieldCollection rangeFields = doc.Fields.Get(doc.Range); foreach (Field currentField in rangeFields) { currentField.ShowCodes = true; FieldsToValues(doc, currentField.CodeRange); } } private void FieldsToValues(Document doc, DocumentRange range) { ReadOnlyFieldCollection rangeFields = doc.Fields.Get(range); foreach (Field currentField in rangeFields) { currentField.ShowCodes = true; FieldsToValues(doc, currentField.CodeRange); string fieldCode = doc.GetText(currentField.CodeRange); if (currentField.Parent == null) continue; string fieldResult = doc.GetRtfText(currentField.ResultRange); DocumentPosition fieldPosition = currentField.Range.Start; doc.Delete(currentField.Range); doc.InsertRtfText(fieldPosition, fieldResult); } }
                                • Oleg (DevExpress Support) 01.10.2018

                                  Hello Sergey,
                                  Thank you for informing me that you have found a way to implement the required behavior.
                                  I tested your code in my sample project and confirm that it correctly replaces nested document fields.
                                  Should you have additional questions regarding our products or need our further assistance, do not hesitate to contact us.

                                • Anders Wang 09.22.2019

                                  Hi,
                                  Is it possible to provide api to get child inner fields from fields?

                                • Mariya (DevExpress Support) 09.23.2019

                                  Hello Anders,
                                  You may check Sergey's code in his previous comment to see the possible approach to access child fields. Use the SubDocument.Fields.Get method to retrieve all fields found in a specific document range and iterate through the returned field collection. If the Field.Parent property is set to null, this field is a parent. If the property value isn't null, this field is a child, and its parent field is specified in the Field.Parent property.