Insert Html into merge field

I have user generated html that I need to insert into a word document merge field while maintaining the formatting from the HTML code. I am using Aspose.Words version 14.7.0.

TIA,
-L

P.S. I am sorry if this question has been already been answered on the forum. The search is timing out for me.

Hi Logan,

Thanks for your inquiry. Sure, you can insert Html during mail merge by using the following code snippet: I hope, this helps.

// File æMailMerge.InsertHtml.docÆ has merge field named æhtmlField1Æ in it.
// File æMailMerge.HtmlData.htmlÆ contains some valid Html data.
// The same approach can be used when merging HTML data from database.
public void MailMergeInsertHtml()
{
    Document doc = new Document(MyDir + "MailMerge.InsertHtml.doc");
    // Add a handler for the MergeField event.
    doc.MailMerge.FieldMergingCallback = new HandleMergeFieldInsertHtml();
    // Load some Html from file.
    StreamReader sr = File.OpenText(MyDir + "MailMerge.HtmlData.html");
    string htmltext = sr.ReadToEnd();
    sr.Close();
    // Execute mail merge.
    doc.MailMerge.Execute(new string[]
    {
        "htmlField1"
    }, new string[]
    {
        htmltext
    });
    // Save resulting document with a new name.
    doc.Save(MyDir + "MailMerge.InsertHtml Out.doc");
}
private class HandleMergeFieldInsertHtml: IFieldMergingCallback
{
    ///

    /// This is called when merge field is actually merged with data in the document.
    ///
    void IFieldMergingCallback.FieldMerging(FieldMergingArgs e)
    {
        // All merge fields that expect HTML data should be marked with some prefix, e.g. æhtmlÆ.
        if (e.DocumentFieldName.StartsWith("html"))
        {
            // Insert the text for this merge field as HTML data, using DocumentBuilder.
            DocumentBuilder builder = new DocumentBuilder(e.Document);
            builder.MoveToMergeField(e.DocumentFieldName);
            builder.InsertHtml((string) e.FieldValue);
            // The HTML text itself should not be inserted.
            // We have already inserted it as an HTML.
            e.Text = "";
        }
    }
    void IFieldMergingCallback.ImageFieldMerging(ImageFieldMergingArgs e)
    {
        // Do nothing.
    }
}

Moreover, I would also suggest you please read the following article that outlines how to apply custom Formatting during Mail Merge:
https://docs.aspose.com/words/net/types-of-mail-merge-operations/

Best regards,

I know this is an old topic, but I found this solution worked great for me when populating a single merge field with HTML content… but when I tried to do the same with 3 fields it would not work for the 2nd and 3rd html merge fields. Does anyone have a solution for implementing more than 1 html mailmerge field? Thanks in advance.

@swimn10s Most likely the problem occurs because there are several merge fields with the same name in your document. To resolve the problem, please try using MoveToField instead of MoveToMergefield method:

private class InsertHtmlFieldMergingCallback : IFieldMergingCallback
{
    public void FieldMerging(FieldMergingArgs args)
    {
        // All merge fields that expect HTML data should be marked with some prefix, e.g.html.
        if (e.DocumentFieldName.StartsWith("html"))
        {
            DocumentBuilder builder = new DocumentBuilder(args.Document);
            builder.MoveToField(args.Field, true);
            builder.InsertHtml((string)args.FieldValue);
            args.FieldValue = null;
        }
    }

    public void ImageFieldMerging(ImageFieldMergingArgs args)
    {
        // Do nothing.
    }
}