Workaround for HTML not supporting merge codes

5 posts, 1 answers
  1. Paul
    Paul avatar
    102 posts
    Member since:
    Jun 2010

    Posted 14 Nov 2012 Link to this post

    According to both of these links:
    http://www.telerik.com/community/forums/wpf/richtextbox/mailmerge-export-to-html.aspx
    http://www.telerik.com/community/forums/silverlight/richtextbox/mail-merge-and-export-as-html-with-merged-tags.aspx

    Merge codes are not supported in HTML.  These are over a year old now.  Will this change?  If not, how can I programatically process a document to fix this?
  2. Paul
    Paul avatar
    102 posts
    Member since:
    Jun 2010

    Posted 15 Nov 2012 Link to this post

    I managed to figure out a work around myself.  If anyone else should find this helpful, I will post it here.  If anyone from Telerik knows a better way or sees flaws, please feel free to post as well!

    This will simply find and fix merge codes when opening an HTML document.  This code is in the DocumentChanged event handler.

    // Fix all merge codes for HTML, start by moving to the top of the document
    editor.Document.CaretPosition.MoveToFirstPositionInDocument();
     
    // Grab all properties in the merge codes - My object type is "clsMergeCodeExample".
    PropertyInfo[] properties = typeof(clsMergeCodeExample).GetProperties();
     
    // Setup a search query
    DocumentTextSearch search = new DocumentTextSearch(editor.Document);
    List<TextRange> rangesTrackingDocumentChanges = new List<TextRange>();
     
    // Define a caret position
    DocumentPosition d = editor.Document.CaretPosition;
     
    // Find the first text range.
    TextRange t = search.Find("{MERGEFIELD", d);
     
    while (t != null)
    {
       // Find the end of the merge code
       TextRange tEnd = search.Find("}", d);
     
       // Combined range
       TextRange tFinal = new TextRange(t.StartPosition, tEnd.EndPosition);
        
       // Find out what the merge code is and remove the } character
       string code = tEnd.StartPosition.GetCurrentInlineBox().Text.Trim().TrimEnd('}');
     
       // Grab this field
       PropertyInfo p = properties.FirstOrDefault(fi => fi.Name.Equals(code, StringComparison.CurrentCultureIgnoreCase));
     
       // Adjust the code as needed. Can be skipped.
       code = (p == null ? "UnknownMergeField" : p.Name);
     
       // Highlight the selection we're about to replace to keep styling in tact
       tFinal.SetSelection(editor.Document);
     
       // Finally insert the merge field as needed replacing the selection.
       editor.Document.InsertField(new MergeField() { PropertyPath = code }, FieldDisplayMode.Code);
     
       // Advance to the next one
       t = search.Find("{MERGEFIELD", d);
    }
  3. UI for WPF is Visual Studio 2017 Ready
  4. Answer
    Mihail
    Admin
    Mihail avatar
    192 posts

    Posted 19 Nov 2012 Link to this post

    Hello Paul,

    We are happy to hear you have found a solution to your problem. At this point, the export of fields in HTML is not planned, so it is not clear when and if it will be implemented.

    If you have any other questions, feel free to contact us.

    Kind regards,
    Mihail
    the Telerik team

    Explore the entire Telerik portfolio by downloading Telerik DevCraft Ultimate.

  5. Thilo
    Thilo avatar
    26 posts
    Member since:
    Feb 2011

    Posted 15 Aug 2014 Link to this post

    Hello Paul !
    this works fine for me !
    I changed it to Docvariable

    But Question to Telerik:
    a Problem is, that RadDocument.InsertField ist declared as Deprecated.
    When will it removed and are there any plan to replace it ?

    My Code:
    private RadDocument ReactivateFieldDOCVARIABLEInHtml(RadDocument document)
    {
        string fieldname = "DOCVARIABLE";
     
        // Fix all merge codes for HTML, start by moving to the top of the document
        document.CaretPosition.MoveToFirstPositionInDocument();
     
        // Setup a search query
        DocumentTextSearch search = new DocumentTextSearch(document);
     
        List<TextRange> rangesTrackingDocumentChanges = new List<TextRange>();
     
        // Define a caret position
        DocumentPosition d = document.CaretPosition;
     
        // Find the first text range.
        TextRange t = search.Find("{" + fieldname, d);
     
        while (t != null)
        {
            // Find the end of the merge code
            TextRange fieldEnd = search.Find("}", d);
     
            // Combined range
            TextRange fieldPosition = new TextRange(t.StartPosition, fieldEnd.EndPosition);
     
            // Find out what the merge code is and remove the } character
            string varName = fieldEnd.StartPosition.GetCurrentInlineBox().Text.Trim().TrimEnd('}');
     
            // Highlight the selection we're about to replace to keep styling in tact
            fieldPosition.SetSelection(document);
     
            // Finally insert the merge field as needed replacing the selection.
            document.InsertField(new DocumentVariableField() { VariableName = varName });
     
            // Advance to the next one
            t = search.Find("{" + fieldname, d);
        }
     
        return document;
    }
           
  6. Petya
    Admin
    Petya avatar
    975 posts

    Posted 19 Aug 2014 Link to this post

    Hi Thilo,

    Indeed, the Insert~() methods of RadDocument are deprecated and we advise you against using them, although I cannot say when they are going to be removed exactly . As to an alternative, you can use the InsertField() method of RadRichTextBox. If for some reason you do not have access to RadRichTextBox (the document is not shown in a control or you are working from your view-model), you could always use the RadDocumentEditor class instead.

    I hope this helps.

    Regards,
    Petya
    Telerik
     
    Check out Telerik Analytics, the service which allows developers to discover app usage patterns, analyze user data, log exceptions, solve problems and profile application performance at run time. Watch the videos and start improving your app based on facts, not hunches.
     
Back to Top
UI for WPF is Visual Studio 2017 Ready