We're sorry Aspose doesn't work properply without JavaScript enabled.

Free Support Forum - aspose.com

Replacement of MergeFieldEventHandler

Hi,
Currently we are consuming the codes from old Aspose.pdf. I am trying to upgrade the codes so that it will be compatible with Aspose.Words 11
What is the replacement for Document.MailMerge.MergeField event handler ? The following code is invalid for Aspose.Words 11

Document docObject = new Document(sDocument);
docObject.MailMerge.MergeField += new MergeFieldEventHandler(MailMerge_MergeField);

I have discovered the following article : https://docs.aspose.com/words/net/
From the above article, it is using InsertDocumentAtMailMergeHandler class.

mainDoc.MailMerge.FieldMergingCallback = new InsertDocumentAtMailMergeHandler();

Can we define the implementation of the IFieldMergingCallback in the same class ? Can you give me the sample code for this ?
Regards,
hadi teo

Hi Hadi,

Thanks for your inquiry. Aspose.Words provides MailMerge.FieldMergingCallback property and this callback occurs during mail merge when a mail merge field is encountered in the document. You need to implement IFieldMergingCallback interface in your class. This interface provides two methods FieldMerging and ImageFieldMerging.

namespace MailMerge_Test
{
    class Program : IFieldMergingCallback
    {
        static void Main(string[] args)
        {
            new Program().Test_MailMerge();
        }
        public void Test_MailMerge()
        {
            string MyDir = @"D:\";
            Document doc = new Document(MyDir + "MailMerge.docx");
            // Add a handler for the MergeField event.
            doc.MailMerge.FieldMergingCallback = this;
            // Execute mail merge.
            doc.MailMerge.Execute(new string[] { "Aspose.Words" }, new string[] { "Aspose.Words for Java" });
            // Save resulting document with a new name.
            doc.Save(MyDir + "AsposeOut.docx");
        }
        // / 
        // / This is called when merge field is actually merged with data in the document.
        // / 
        void IFieldMergingCallback.FieldMerging(FieldMergingArgs e)
        {
            // Your Code
            if (e.FieldName == "Aspose.Words")
            {
                e.Text = "Aspose.Words for .NET";
            }
        }
        void IFieldMergingCallback.ImageFieldMerging(ImageFieldMergingArgs e)
        {
            // Do nothing.
        }
    }
}

Following code example shows how to mail merge HTML data into a document.

// 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.
    }
}