MailMerge with ExpandoObject

4 posts, 0 answers
  1. Tomasz Zawadzki
    Tomasz Zawadzki avatar
    2 posts
    Member since:
    Apr 2010

    Posted 14 Oct Link to this post

    Hi,

    I've found a problem with a MailMerge and a list ExpandoObject as datasource. After switching to Result display mode, it appears to replace the MergefFields with string.empty.

    Dynamic object with "static written" property works fine.

    Problem seems to be the same as described here: http://www.telerik.com/forums/dynamic-mailmerge

    Please apply the same fix in WinForms.

    Regards, Tomasz.

  2. Dimitar
    Admin
    Dimitar avatar
    1404 posts

    Posted 17 Oct Link to this post

    Hi Tomasz,

    Thank you for writing. 

    Indeed this is currently not working in the WinForms RichTextEditor. I have logged a feature request for it in our Feedback Portal. I have added a vote for it on your behalf as well. You can track its progress, subscribe for status changes and add your comment to it here. I have also updated your Telerik Points.

    In addition, I have attached a sample project that shows how you can implement something similar. 

    Please do not hesitate to contact us with any additional questions or concerns. 

    Regards,
    Dimitar
    Telerik by Progress
    Check out the Windows Forms project converter, which aids the conversion process from standard Windows Forms applications written in C# or VB to Telerik UI for WinForms.For more information check out this blog post and share your thoughts.
  3. UI for WinForms is Visual Studio 2017 Ready
  4. Tomasz Zawadzki
    Tomasz Zawadzki avatar
    2 posts
    Member since:
    Apr 2010

    Posted 18 Oct in reply to Dimitar Link to this post

    Hi Dimitar,

    Ok, I can build and run your sollution and indeed it shows that you can insert custom mergefields in doc file by UI (what was not exactly the point) BUT... if you insert those fields by UI and hit "Preview Results" the mergefielgs are replaced by nothing (string.empty) instead of strings defined in code - what is exactly the point :)

    Thx for feature request (imo bug request btw).

    Regards, Tomasz.
  5. Dimitar
    Admin
    Dimitar avatar
    1404 posts

    Posted 18 Oct Link to this post

    Hello Tomasz,

    Yes, I can confirm that the merge operation is not working. It appears that the example only shows how you can replace the insert merge field button. So to make this work you need to create a custom merge field and return a proper value: 
    class MyMergeField : MergeField
    {
        private static readonly string FieldType = "DYNAMIC_FIELD";
        static MyMergeField()
        {
            CodeBasedFieldFactory.RegisterFieldType(MyMergeField.FieldType, () => new MyMergeField());
        }
        public override Field CreateInstance()
        {
            return new MyMergeField();
        }
        public override string FieldTypeName
        {
            get
            {
                return MyMergeField.FieldType;
            }
        }
        protected override DocumentFragment GetResultFragment()
        {
            //  return base.GetResultFragment();
     
            string valueAsString = null;
     
            if (this.Document != null && !string.IsNullOrEmpty(this.PropertyPath))
            {
                object currentItem = this.Document.MailMergeDataSource.CurrentItem;
                if (currentItem != null)
                {
                    object value = "";
                    var dataObject = currentItem as MyDynamicObject;
                    value = dataObject.Get(this.PropertyPath);
                    valueAsString =  this.ConvertToString(value);
     
                }
            }
     
            if (!string.IsNullOrEmpty(valueAsString))
            {
                valueAsString = this.TextBeforeIfNotEmpty + valueAsString + this.TextAfterIfNotEmpty;
            }
     
            DocumentFragment resultFragment = base.CreateFragmentFromText(valueAsString);
            return resultFragment;
        }
    }

    I hope this will be useful. 

    Regards,
    Dimitar
    Telerik by Progress
    Check out the Windows Forms project converter, which aids the conversion process from standard Windows Forms applications written in C# or VB to Telerik UI for WinForms.For more information check out this blog post and share your thoughts.
Back to Top