Hi,
I am using Mail Merge fields to write address on an envelope. Here is an example:
John Smith
125 Main St
Toronto, ON M2R 3B7
Canada
Sometimes there could be 2 lines for the address, for example:
John Smith
5th Floor
125 Main St
Toronto, ON M2R 3B7
Canada
Below is how I have setup document in RichTextBox.
{MERGEFIELD FullName}
{MERGEFIELD Address1}
{MERGEFIELD Address2}
{MERGEFIELD City}, {MERGEFIELD Province} {MERGEFIELD PostalCode}
{MERGEFIELD Country}
But if Address2 is empty, I don't want to have an empty line. I need to setup Address2 mail merge field in such a way, so that if it is empty, it does not take up the line and City/Province/PostalCode displayed on the 3rd line.
Anyone can suggest how to achieve this either through the template or through the backend?
8 Answers, 1 is accepted
Please find attached a sample project demonstrating how you can achieve the desired result using the TextAfterIfNotEmpty property of merge fields. For this purpose the fields are added on a single line and a line break is added to the mentioned property. If you preview the results of the mail merge and change the current item you can see how this behaves.
I hope this is helpful!
Regards,
Petya
Telerik
Learn what features your users use (or don't use) in your application. Know your audience. Target it better. Develop wisely.
Sign up for Free application insights >>
Hi,
Thank you for this example. The result is exactly what I am looking for. However, it looks like you have hardcoded the TextAfterIfNotEmpty property into a template with some weird character TextAfterIfNotEmpty="¬". What is this character?
In my program, user can modify the template. I want to give this option to the user. I noticed that once I switch to "Show All Fields Codes" mode, it looks like this:
{MERGEFIELD Address1 \f
}{MERGEFIELD Address2 \f
}{MERGEFIELD Address3 \f
}Text after fields
When I add field myself and add '\f' to it, this flag is not saved.
I also tried adding button to the ribbon bar and On Click inserting special character into the field using editor.Insert("¬");. But now I can see the new line, but no \f. So I can see that I am missing something...
Maybe I misunderstood your example, can you clarify how users can set TextAfterIfNotEmpty property of the field themselves?
Thank you,
Michael
You can insert a merge field with its TextAfterIfNotEmpty property set in the following manner:
this
.editor.InsertField(
new
MergeField() { TextAfterIfNotEmpty =
"¬"
, PropertyPath =
"Address1"
}, FieldDisplayMode.DisplayName);
The symbol used in this approach is the one which is interpreted as line break from RadRichTextBox. As to the flag, it indicates that the TextAfterIfNotEmpty property is set and following it is its content.
As to allowing users to insert their fields in the same manner from RadRichTextBox's UI, you could subscribe to the CommandExecuting event of RadRichTextBox and handle the insertion of merge fields like this:
if
(e.Command
is
InsertFieldCommand)
{
MergeField field = (e.CommandParameter
as
MergeField);
if
(field !=
null
)
{
if
(field.PropertyPath.StartsWith(
"Address"
))
{
e.Cancel =
true
;
this
.editor.InsertField(
new
MergeField() { TextAfterIfNotEmpty =
"¬"
, PropertyPath =
"Address1"
}, FieldDisplayMode.Code);
}
}
}
Please note that this solution is merely a workaround as the scenario is currently not natively supported by RadRichTextBox. Such requirements will be supported once IF fields, which are already in our to-do list, are implemented.
Let me know if you have further questions.
Regards,
Petya
Telerik
Learn what features your users use (or don't use) in your application. Know your audience. Target it better. Develop wisely.
Sign up for Free application insights >>
Have you implemented the IF option the merged field? If you did, can you show some example here?
Michael
The functionality has not been implemented yet and I cannot commit to any certain time-frame when it might be available. I suggest you follow the public item for this in our Ideas & Feedback portal to be notified about any progress on the matter: Add support for IF and ASK fields.
Regards,
Petya
Telerik
Hi,
Since last update, "¬" character does not work anymore. It showed up on my old template instead of being used as optional next line. Did something change recently? did you implement the some sort of "IF" option in Mail Merge?
I just want to add to my comment above. I re-downloaded the sample you provided in the past and updated Telerik components. It is not working correctly. This is what I see on the merged document
1 High Street¬London¬Text after fields
We already discussed this in the support ticket you submitted on the matter, but I'll add the information here as well in case anyone else encounters an issue.
As of Q1 2015 the workaround provided earlier in this thread doesn't function due to a change in the way RadRichTextBox handles the line break symbol. For the time being we're unable to propose a solution, but implementing this feature should allow us to do so: Threat Vertical Tab symbol ('', also '\v' in .NET) as line break.
Regards,
Petya
Telerik