# Removing MERGEFIELD Codes While Replacing Placeholders with Values in RadWordsProcessing

Remove merge-field codes and replace their placeholders with plain-text values in a Word document.

## Environment

| Version    | Product            | Author                                                                          |
| ---------- | ------------------ | ------------------------------------------------------------------------------- |
| 2024.3.806 | RadWordsProcessing | [Desislava Yordanova](https://www.telerik.com/blogs/author/desislava-yordanova) |

## Description

When preparing a document for the [MailMerge](https://www.telerik.com/document-processing-libraries/documentation/libraries/radwordsprocessing/editing/mail-merge) operation in RadWordsProcessing, you may need to remove a [MERGEFIELD](https://www.telerik.com/document-processing-libraries/documentation/libraries/radwordsprocessing/concepts/fields/merge-field) without losing its inserted value. This process keeps the document clean and ready for MailMerge without issues related to leftover `MERGEFIELD` entries.

This KB article also answers the following questions:

* How can I delete `MERGEFIELD` fields but keep their content in the document?
* What is the correct approach to prepare a document for MailMerge in RadWordsProcessing?
* Is there a way to clean up `MERGEFIELD` entries from a document without affecting its content?

## Solution

To remove a `MERGEFIELD` while keeping its values, use the `RadFlowDocumentEditor`'s `DeleteContent` method. This approach deletes the field codes but leaves the field values as plain text in the document. The [Find and Replace](https://www.telerik.com/document-processing-libraries/documentation/libraries/radwordsprocessing/editing/find-and-replace/find-and-replace-text) feature provides a straightforward way to handle any leftover text from the fields.

The following example shows how to accomplish this:

```csharp
using Telerik.Windows.Documents.Flow.Model.Editing;
using Telerik.Windows.Documents.Flow.Model;
using Telerik.Windows.Documents.Flow.Model.Fields;
using Telerik.Windows.Documents.Flow.FormatProviders.Docx;
using System.Diagnostics;

RadFlowDocument document = new RadFlowDocument();
RadFlowDocumentEditor editor = new RadFlowDocumentEditor(document);
FieldInfo firstNameField = editor.InsertField("MERGEFIELD First_Name", "«first name»");
FieldInfo lastNameField = editor.InsertField("MERGEFIELD Last_Name", "«last name»");

//Get field characters of merged fields
var fieldCharacters = document.EnumerateChildrenOfType<FieldCharacter>().Where(ch => ch.FieldInfo.Field is MergeField).ToList();

//Get field infos of merged fields
var fieldInfos = fieldCharacters.Select(fc => fc.FieldInfo).Distinct();

//Iterate and delete merge fields
foreach (var fieldInfo in fieldInfos)
{
    editor.DeleteContent(fieldInfo.Start, fieldInfo.Separator, true);
    editor.DeleteContent(fieldInfo.End, fieldInfo.End, true);
}

//Replace the text that is left
editor.ReplaceText("«first name»", "John ");
editor.ReplaceText("«last name»", "Smith ");


//Export document
DocxFormatProvider provider = new DocxFormatProvider();
string outputFilePath = "output.docx";
File.Delete(outputFilePath);
using (Stream output = File.OpenWrite(outputFilePath))
{
    provider.Export(document, output);
}

//Open document
Process.Start(new ProcessStartInfo() { FileName = outputFilePath, UseShellExecute = true });
```

This method removes the `MERGEFIELD` effectively, leaving only the value in the document.

Before replacement, the document contains `MERGEFIELD` entries for the first and last names:

![Merge Fields Before](https://www.telerik.com/document-processing-libraries/documentation/assets/b99eb2da11cffaaa852e36f1017691df/mergefields.gif)

After replacement, the document contains the plain-text values `John` and `Smith`:

![Document after replacing the first-name and last-name merge fields with John Smith.](https://www.telerik.com/document-processing-libraries/documentation/assets/396f8780e663ad792388db6944cb6101/replacedmergefields.png)

## See Also

* [Find and Replace Text and Style in RadWordsProcessing](https://www.telerik.com/document-processing-libraries/documentation/libraries/radwordsprocessing/editing/find-and-replace/find-and-replace-text)
* [RadFlowDocumentEditor Class Overview](https://www.telerik.com/document-processing-libraries/documentation/libraries/radwordsprocessing/editing/radflowdocumenteditor)
* [FieldCharacter Class in RadWordsProcessing](https://www.telerik.com/document-processing-libraries/documentation/libraries/radwordsprocessing/model/fieldcharacter)
