How the access the MergeFields in a Word document

5 Answers 705 Views
RichTextEditor
Will
Top achievements
Rank 1
Will asked on 27 May 2021, 04:17 PM

L.S.

I am using OpenEdge ABL version 12.2.4.

I am aware of the default way of using merge fields (https://docs.telerik.com/devtools/winforms/controls/richtexteditor/features/mail-merge/mail-merge).

In ABL, all our custom classes by definiton inherit from Progress.Lang.Object. Unfortunately, it is not possible to use an ABL class as the argument class in the definition of a generic class. 

This works of course c#: List<Employee> employees = new List<Employee>()

In ABL it would look like this:

DEFINE VARIABLE employees AS "List<Employee>" NO-UNDO.
employees = NEW "List<Employee>"().

However, this does not compile.

So, I need a way to access the individual Mail Merge fields.

 

Any support is appreciated,

Regards,

Will


5 Answers, 1 is accepted

Sort by
0
Dimitar
Telerik team
answered on 31 May 2021, 06:22 AM

Hello Will,

If I understand correctly you need to manually pass the data to the merge field when the mail merge is performed because you cannot set the data source like in C#. To achieve this you can create a custom merge field and pass the data manually. An example of this is available in our SDK repository: winforms-sdk/OrdersMergeField.cs at master · telerik/winforms-sdk. Could you please check it and let me know if this can be translated to ABL.

I am looking forward to your reply.

Regards,
Dimitar
Progress Telerik

Virtual Classroom, the free self-paced technical training that gets you up to speed with Telerik and Kendo UI products quickly just got a fresh new look + new and improved content including a brand new Blazor course! Check it out at https://learn.telerik.com/.

Will
Top achievements
Rank 1
commented on 31 May 2021, 09:35 AM

You understand correctly.
But I don't understand why I should create a merge field, while they are already present in the document; I only need to access them, or am I missing something here.
0
Dimitar
Telerik team
answered on 01 Jun 2021, 10:54 AM

Hello Will,

I suggested this approach because when the Merge fields are evaluated the GetResultFragment method will be called and you can pass the result dynamically. In this method, you have access to the filed properties as well. 

It is possible to get the merge fields as well. You can use the following approach for this: 

private void radButton1_Click(object sender, EventArgs e)
{
    var fieldRanges = radRichTextEditor1.Document.EnumerateChildrenOfType<FieldRangeStart>();
    foreach (var range in fieldRanges)
    {
        var mergeField = range.Field as MergeField;
        Console.WriteLine(mergeField.PropertyPath);
    }

}

Let me know if I can assist you further.

Regards,
Dimitar
Progress Telerik

Love the Telerik and Kendo UI products and believe more people should try them? Invite a fellow developer to become a Progress customer and each of you can get a $50 Amazon gift voucher.

Will
Top achievements
Rank 1
commented on 01 Jun 2021, 01:15 PM

Method EnumerateChildrenOfType does not exist in class Telerik.WinForms.Documents.Model.RadDocument. Do you have an alternative way to access the merge fields.
Lance | Manager Technical Support
Telerik team
commented on 01 Jun 2021, 04:29 PM

Hi Will, EnumerateChildrenOfType is an extension method that lives in Telerik.WinControls.UI. The reason you don't add the using statment i sbecause the Form imports it already. If you wanted to do it purely in C#, then be sure to add `using Telerik.WinControls.UI;`

Alternate Option

You can take an alternate approach and just enumerate the document's children and check the type inside the foreach loop:

foreach (var child in radRichTextEditor1.Document.Children)
{
    // check the child's Type
    if (child is FieldRangeStart range)
    {
        var mergeField = range.Field as MergeField;
        Console.WriteLine(mergeField.PropertyPath);
    }
}
Will
Top achievements
Rank 1
commented on 02 Jun 2021, 10:04 AM

Hi Dimitar,

I think you are referring to a RadFlowDocument with method EnumerateChildrenOfType inherited from DocumentElementBase. I don't have a RadFlowDocument or a DocumentElementBase class.
I am using 2021R1 (Version=2021.1.326.40).

I recoded your Alternate Option in 4GL (see below).
I use document DocxTestDocument.docx, which contains several MergeFields, which are visible when I open the document in Word.
However, when I run the code below, the RadDocument only seems to have 1 child, which cannot be cast to a FieldRangeStart type.

Still confused.
Regards,
Will

USING Telerik.WinForms.Documents.Model.* FROM ASSEMBLY.
USING Telerik.WinForms.Documents.FormatProviders.OpenXml.Docx.* FROM ASSEMBLY.
USING System.Collections.Generic.* FROM ASSEMBLY.

DEFINE VARIABLE Document AS RadDocument NO-UNDO.
DEFINE VARIABLE ChildEnumerator AS "IEnumerator<DocumentElement>" NO-UNDO.
DEFINE VARIABLE DocumentElement AS DocumentElement NO-UNDO.
DEFINE VARIABLE Range AS FieldRangeStart NO-UNDO.
DEFINE VARIABLE MergeField AS MergeField NO-UNDO.
DEFINE VARIABLE oField AS Field NO-UNDO.

Document = ( NEW DocxFormatProvider()):Import
( System.IO.File:OpenRead( "C:\Users\Gebruiker\Downloads\DocxTestDocument.docx" )).

ChildEnumerator = Document:Children:GetEnumerator().

DO WHILE ChildEnumerator:MoveNext():

DocumentElement = ChildEnumerator:Current.

Range = CAST( DocumentElement, FieldRangeStart ) NO-ERROR.
IF NOT VALID-OBJECT( Range ) THEN
NEXT.
MergeField = CAST( Range:Field, MergeField ).

MESSAGE MergeField:PropertyPath
VIEW-AS ALERT-BOX.
END.
0
Dimitar
Telerik team
answered on 03 Jun 2021, 12:21 PM

Hello Will,

The EnumerateChildrenOfType method should be available in the WinForms suite as well. I am not sure why it is not available in this case. 

Nevertheless, the method should recursively iterate all children. The first child is the section that holds all other elements. Here is an example in C#: 

public void EnumerateChildrenOfType(DocumentElement element, List<FieldRangeStart> fields)
{
    if (element is FieldRangeStart)
    {
        fields.Add((FieldRangeStart)element);
    }

    foreach (DocumentElement child in element.Children)
    {
        EnumerateChildrenOfType(child, fields);
    }

}

Let me know if I can assist you further.

Regards,
Dimitar
Progress Telerik

Virtual Classroom, the free self-paced technical training that gets you up to speed with Telerik and Kendo UI products quickly just got a fresh new look + new and improved content including a brand new Blazor course! Check it out at https://learn.telerik.com/.

Will
Top achievements
Rank 1
commented on 03 Jun 2021, 06:08 PM

Hi Dimitar,
Can you supply me with the package and class name where method EnumerateChildrenOfType is located, so I can check whether it is there or not; perhaps I am missing something; wouldn't be the first time ;-)
0
Dimitar
Telerik team
answered on 04 Jun 2021, 08:51 AM

Hello Will,

Sure, the assebly is Telerik.WinControls.RichTextEditor and namspaces is Telerik.WinForms.Documents.Model.

Let me know if I can assist you further.

Regards,
Dimitar
Progress Telerik

Love the Telerik and Kendo UI products and believe more people should try them? Invite a fellow developer to become a Progress customer and each of you can get a $50 Amazon gift voucher.

Will
Top achievements
Rank 1
commented on 04 Jun 2021, 09:53 AM

Hi Dimitar,

This is what I use:
- Telerik.WinControls.UI.RadRichTextEditor, its Document property refers to class
- Telerik.WinForms.Documents.Model.RadDocument.
The RadDocument class however, does not contain method EnumerateChildrenOfType.

Just to be sure: This is the list of dll's I use:
- Telerik.WinControls, Version=2016.3.1024.40
- Telerik.WinControls.RichTextEditor, Version=2016.3.1024.40
- Telerik.WinControls.UI, Version=2016.3.1024.40
- Telerik.WinControls.UI.Design, Version=2021.1.326.40
- TelerikCommon, Version=2021.1.326.40
- TelerikData, Version=2021.1.326.4

I am getting the impression, we're talking about different implementations.

Hope to hear from you soon,
Regards,
Will
0
Dimitar
Telerik team
answered on 07 Jun 2021, 07:05 AM

Hi Will,

I have tested this with the version that you have provided and the method is still availble. I can recall that there were problems with generics in ABL and even they were not supported. Is this still true? I would suggest opening a ticket for this for OpenEdge in SalesForce asking for the possible reason for the missing method.  

Let me know if I can assist you further.

Regards,
Dimitar
Progress Telerik

Love the Telerik and Kendo UI products and believe more people should try them? Invite a fellow developer to become a Progress customer and each of you can get a $50 Amazon gift voucher.

Will
Top achievements
Rank 1
commented on 08 Jun 2021, 09:28 AM

I'll do that and get back to you
Tags
RichTextEditor
Asked by
Will
Top achievements
Rank 1
Answers by
Dimitar
Telerik team
Share this question
or