This is a migrated thread and some comments may be shown as answers.

Changing case of mail merge fields

9 Answers 365 Views
RichTextBox
This is a migrated thread and some comments may be shown as answers.
Troy
Top achievements
Rank 1
Troy asked on 07 May 2015, 03:57 PM

Hi,

  There are switches you can use in Word to make the value of a mail merge field change case.  Ex. \* FirstCap, \* Upper, \* lower.  These don't seem to work correctly in the RichTextBox.  I'm adding the fields in code and the values display but the formatting isn't correct.  I've taken a template I made in Word and tried that which works correctly in Word but doesn't in the Telerik control so I'm not sure it is my code that is the issue.

  Any help would be appreciated.

      Troy

9 Answers, 1 is accepted

Sort by
0
Tanya
Telerik team
answered on 12 May 2015, 02:57 PM
Hello,

I am pasting the answer of this question in case somebody else needs it:

"Currently these switches are not supported in the RadRichTextBox. We have already logged this request and you could vote and subscribe to the public item in the feedback portal in order to receive updates about status changes on it. 

In order to achieve your goal you could implement a custom MergeField. I am attaching a simple project, which demonstrates how such field could be implemented to support the "\* upper" switch
."

Regards,
Tanya
Telerik
 

See What's Next in App Development. Register for TelerikNEXT.

 
0
Brian Stanek
Top achievements
Rank 2
answered on 07 Dec 2015, 09:50 PM

Is there a way I can make the formatting code {EQ \a ({MERGEFIELD Names})} to work in RadRichTextBox to display names separated by commas in multiple lines? I have verified that this works in MS Word. Thanks.

0
Boby
Telerik team
answered on 08 Dec 2015, 07:45 AM
Hello Brian,

EQ field is not supported out of the box, moreover it seems kind of deprecated now as it's not included in the MS Word fields help article nor in the last editions of the Office Open XML (sure Word still supports it for backward compatibility).

It should be somewhat tricky, but possible, to implement the EQ functionality you want as a custom field. You can use the tutorial in the Custom Field help article that Tanya linked. Other resource that might be helpful is the Custom Field SDK demo in our xaml-sdk repo on GitHub.

Basically you should create a FieldProperty (and a corresponding CLR property) for each of the switches. Then, based on the values of the switches, you can create proper DocumentFragment by overriding the GetResultFragment method. 

Regards,
Boby
Telerik
Do you want to have your say when we set our development plans? Do you want to know when a feature you care about is added or when a bug fixed? Explore the Telerik Feedback Portal and vote to affect the priority of the items
0
Brian Stanek
Top achievements
Rank 2
answered on 09 Dec 2015, 04:42 PM

Hi Boby,

Thanks for your prompt reply. Using Custom Field for mail merge, it works great in the RadRichTextBox. However, I am actually doing mail merge programmatically using RadFlowDocument.MailMerge() method. Is there a way to handle the custom mail merge fields programmatically?

Cheers,

Brian

 

0
Boby
Telerik team
answered on 10 Dec 2015, 03:17 PM
Hi Brian ,

I am confused. You mentioned RadFlowDocument.MailMerge, which is the document class from WordsProcessing (located in Telerik.Windows.Documents.Flow). On contrary, RadRichTextBox uses RadDocument (located in Telerik.Windows.Documents).

RadDocument supports the concept for custom fields, and my suggestions was for its API. RadWordsProcessing API currently doesn't allow updating of custom fields, though you can insert them in the document and they are preserved on import/export to DOCX (of course you can manually "update" them by replacing their result fragment directly in the document model).

Could you clarify what is your scenario and which components are you using?

Regards,
Boby
Telerik
Do you want to have your say when we set our development plans? Do you want to know when a feature you care about is added or when a bug fixed? Explore the Telerik Feedback Portal and vote to affect the priority of the items
0
Brian Stanek
Top achievements
Rank 2
answered on 10 Dec 2015, 06:31 PM

Hi Boby,

When I manually update the merge fields using result fragment, is there a way to add a carriage return/line break? I am using RadFlowDocumentEditor to modify a RadFlowDocument progammatically where I need to insert another document, add header and footer and perform mail merge. And I also need to support the mail merge fields to display data in multiple lines. 

Thanks.

0
Boby
Telerik team
answered on 11 Dec 2015, 11:38 AM
Hi Brian,

First I want to mention that multiline field result should be supported out of the box, however the current implementation has bug in it, namely this one:
Mail merge doesn't respect new lines from mail merge source

If you are manually replacing merge fields with their result, RadDocumentEditor won't help, as it's purpose is to do more high-level interactions. Instead you could use directly the document model to remove the field inlines and replace them with the result. For example, the following will remove the merge-field-associated inlines for the first merge field.

// this is just an example on how to get the first merge field.
var fieldStart = document.EnumerateChildrenOfType<FieldCharacter>()
    .Where(fc => fc.FieldCharacterType == FieldCharacterType.Start && fc.FieldInfo.Field is MergeField)
    .First();
 
var fieldEnd = fieldStart.FieldInfo.End;
 
var parentParagraph = fieldStart.Paragraph;
 
List<InlineBase> inlinesToRemove = new List<InlineBase>();
 
bool found = false;
 
foreach (var inline in parentParagraph.Inlines)
{
    if (!found && inline == fieldStart)
    {
        found = true;
    }
 
    if (found)
    {
        inlinesToRemove.Add(inline);
 
        if (inline == fieldEnd)
        {
            break;
        }
    }
}
 
foreach (var inline in inlinesToRemove)
{
    parentParagraph.Inlines.Remove(inline);
}

If you want to continue our conversation further, I would suggest you to open a dedicated support thread for RadWordsProcessing or continue in the RadWordsProcessing forum, as this forum is dedicated to RadRichTextBox.

Regards,
Boby
Telerik
Do you want to have your say when we set our development plans? Do you want to know when a feature you care about is added or when a bug fixed? Explore the Telerik Feedback Portal and vote to affect the priority of the items
0
Vance Smith
Top achievements
Rank 1
answered on 16 Nov 2017, 04:02 AM

Hi, I have an existing Word document set up for mail merge. In other words, merge fields are already in the document. How do I mail merge to this kind of document. I am able to load the existing document into a RadFlowDocument, and then call MailMerge(data), but the mail merge does not occur.

private static void TestMailMerge()
        {
            List<MailMergeRecord> mailMergeDataSource = new List<MailMergeRecord>()
            {
                new MailMergeRecord()
                {
                    Test = "test-string",
                    SalesTaxLicenseNumber = "stnumbertest"
                },
                new MailMergeRecord()
                {
                    Test = "test-string-2",
                    SalesTaxLicenseNumber = "stnumbertest2"
                },
            };
 
            RadFlowDocument document;
            var fileFormatProvider = new DocxFormatProvider();
            string fileName = System.IO.Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "TrashMaybe\\Waiver-Denied 4-10.docx");
 
            using (FileStream input = new FileStream(fileName, FileMode.Open))
            {
                document = fileFormatProvider.Import(input);
            }
            document.MailMerge(mailMergeDataSource);
            document.UpdateFields();
 
 
            using (Stream output = new FileStream("C:\\temp\\output.docx", FileMode.OpenOrCreate))
            {
                fileFormatProvider.Export(document, output);
            }
        }
0
Boby
Telerik team
answered on 16 Nov 2017, 02:52 PM
Hello Vance,

RadFlowDocument.MailMerge(*) method returns a new instance of RadFlowDocument, instead of modifying the template.

Also this is the forum for RadRichTextBox, and you are using RadFlowDocument and its mail merge functionality, which is part of RadWordsProcessing. If you want to use the RadRichTextBox-related classes (which has more features available, but could be slower), you should instead use RadDocument and its mail merge capabilities.

Regards,
Boby
Progress Telerik
Want to extend the target reach of your WPF applications, leveraging iOS, Android, and UWP? Try UI for Xamarin, a suite of polished and feature-rich components for the Xamarin framework, which allow you to write beautiful native mobile apps using a single shared C# codebase.
Tags
RichTextBox
Asked by
Troy
Top achievements
Rank 1
Answers by
Tanya
Telerik team
Brian Stanek
Top achievements
Rank 2
Boby
Telerik team
Vance Smith
Top achievements
Rank 1
Share this question
or