Skip to content

Commit fbc208f

Browse files
Merge pull request #472 from telerik/new-kb-remove-mergefields-retain-values-radwordsprocessing-e10b03c570574651b63ad551f5bab7a3
Added new kb article remove-mergefields-retain-values-radwordsprocessing
2 parents 3c87d75 + dc4a414 commit fbc208f

File tree

4 files changed

+85
-0
lines changed

4 files changed

+85
-0
lines changed

knowledge-base/images/mergeFields.gif

23.9 KB
Loading
518 Bytes
Loading
Lines changed: 84 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,84 @@
1+
---
2+
title: How to Remove a MERGEFIELD While Replacing the Placeholders with Values in RadWordsProcessing
3+
description: Learn how to remove a MERGEFIELD from a document while replacing the placeholders with actual values, facilitating the MailMerge process in RadWordsProcessing.
4+
type: how-to
5+
page_title: How to Remove a MERGEFIELD WWhile Replacing the Placeholders with Values in RadWordsProcessing
6+
slug: remove-mergefields-retain-values-radwordsprocessing
7+
tags: wordsprocessing, document, processing, merge, field, mailmerge, remove
8+
res_type: kb
9+
ticketid: 1667593
10+
---
11+
12+
## Environment
13+
14+
| Version | Product | Author |
15+
| --- | --- | ---- |
16+
| 2024.3.806| RadWordsProcessing |[Desislava Yordanova](https://www.telerik.com/blogs/author/desislava-yordanova)|
17+
18+
## Description
19+
When preparing a document for the [MailMerge]({%slug radwordsprocessing-editing-mail-merge%}) operation in RadWordsProcessing, it might be necessary to remove a [MERGEFIELD]({%slug radwordsprocessing-concepts-merge-field%}) without losing its inserted value. This process ensures that the document is clean and ready for MailMerge without encountering issues related to leftover `MERGEFIELD` .
20+
21+
This KB article also answers the following questions:
22+
- How can I delete `MERGEFIELD` fields but keep their content in the document?
23+
- What is the correct approach to prepare a document for MailMerge in RadWordsProcessing?
24+
- Is there a way to clean up `MERGEFIELD` from a document without affecting its content?
25+
26+
## Solution
27+
To remove a `MERGEFIELD` while retaining their values, you can use the `RadFlowDocumentEditor`'s `DeleteContent` method. This approach involves deleting the field codes but leaving the field values as plain text in the document. Additionally, the [Find and Replace]({%slug radwordsprocessing-editing-find-and-replace%}) functionality provides a straightforward way to handle any leftover text from the fields.
28+
29+
Here is an example code snippet demonstrating how to accomplish this:
30+
31+
```csharp
32+
using Telerik.Windows.Documents.Flow.Model.Editing;
33+
using Telerik.Windows.Documents.Flow.Model;
34+
using Telerik.Windows.Documents.Flow.Model.Fields;
35+
using Telerik.Windows.Documents.Flow.FormatProviders.Docx;
36+
using System.Diagnostics;
37+
38+
RadFlowDocument document = new RadFlowDocument();
39+
RadFlowDocumentEditor editor = new RadFlowDocumentEditor(document);
40+
FieldInfo firstNameField = editor.InsertField("MERGEFIELD First_Name", "«first name»");
41+
FieldInfo lastNameField = editor.InsertField("MERGEFIELD Last_Name", "«last name»");
42+
43+
//Get field characters of merged fields
44+
var fieldCharacters = document.EnumerateChildrenOfType<FieldCharacter>().Where(ch => ch.FieldInfo.Field is MergeField).ToList();
45+
46+
//Get field infos of merged fields
47+
var fieldInfos = fieldCharacters.Select(fc => fc.FieldInfo).Distinct();
48+
49+
//Iterate and delete merge fields
50+
foreach (var fieldInfo in fieldInfos)
51+
{
52+
editor.DeleteContent(fieldInfo.Start, fieldInfo.Separator, true);
53+
editor.DeleteContent(fieldInfo.End, fieldInfo.End, true);
54+
}
55+
56+
//Replace the text that is left
57+
editor.ReplaceText("«first name»", "John ");
58+
editor.ReplaceText("«last name»", "Smith ");
59+
60+
61+
//Export document
62+
DocxFormatProvider provider = new DocxFormatProvider();
63+
string outputFilePath = "output.docx";
64+
File.Delete(outputFilePath);
65+
using (Stream output = File.OpenWrite(outputFilePath))
66+
{
67+
provider.Export(document, output);
68+
}
69+
70+
//Open document
71+
Process.Start(new ProcessStartInfo() { FileName = outputFilePath, UseShellExecute = true });
72+
```
73+
74+
This method ensures that the `MERGEFIELD ` is removed effectively, leaving only the value in the document.
75+
76+
|Before|After|
77+
|----|----|
78+
|![Merge Fields Before](images/mergeFields.gif)|![Merge Fields After](images/replacedMergeFields.png)|
79+
80+
## See Also
81+
82+
- [Find and Replace Text and Style in RadWordsProcessing]({%slug radwordsprocessing-editing-find-and-replace%})
83+
- [RadFlowDocumentEditor Class Overview]({%slug radwordsprocessing-editing-radflowdocumenteditor%})
84+
- [FieldCharacter Class in RadWordsProcessing]({%slug radwordsprocessing-model-fieldcharacter%})

libraries/radwordsprocessing/editing/mail-merge.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -226,4 +226,5 @@ If you want to separate the items into several rows you need to close the group
226226
* [Inserting Images using Mail Merge]({%slug inserting-images-using-mail-merge-radwordsprocessing%})
227227
* [Populate a Table with Data using Nested Mail Merge Functionality]({%slug populate-table-data-mail-merge%})
228228
* [Generating a Word Document Template with Data Using MailMerge in RadWordsProcessing]({%slug generate-doc-template-and-populate-with-collection-data-mail-merge%})
229+
* [How to Remove a MERGEFIELD While Replacing the Placeholders with Values in RadWordsProcessing]({%slug remove-mergefields-retain-values-radwordsprocessing%})
229230
* [Performing Nested MailMerge with Multiple Levels in RadWordsProcessing]({%slug nested-mailmerge-radwordsprocessing%})

0 commit comments

Comments
 (0)