FieldMergingArgs Class
Provides data for the MergeField event.
Inheritance Hierarchy

Namespace: Aspose.Words.MailMerging
Assembly: Aspose.Words (in Aspose.Words.dll) Version: 19.9
Syntax
public class FieldMergingArgs : FieldMergingArgsBase

The FieldMergingArgs type exposes the following members.

Properties
  NameDescription
Public propertyCode exampleDocument
Returns the Document object for which the mail merge is performed.
(Inherited from FieldMergingArgsBase.)
Public propertyCode exampleDocumentFieldName
Gets the name of the merge field as specified in the document.
(Inherited from FieldMergingArgsBase.)
Public propertyCode exampleField
Gets the object that represents the current merge field.
(Inherited from FieldMergingArgsBase.)
Public propertyCode exampleFieldName
Gets the name of the merge field in the data source.
(Inherited from FieldMergingArgsBase.)
Public propertyCode exampleFieldValue
Gets the value of the field from the data source.
(Inherited from FieldMergingArgsBase.)
Public propertyCode exampleRecordIndex
Gets the zero based index of the record that is being merged.
(Inherited from FieldMergingArgsBase.)
Public propertyCode exampleTableName
Gets the name of the data table for the current merge operation or empty string if the name is not available.
(Inherited from FieldMergingArgsBase.)
Public propertyCode exampleText
Gets or sets the text that will be inserted into the document for the current merge field.
Methods
  NameDescription
Public methodEquals
Determines whether the specified Object is equal to the current Object.
(Inherited from Object.)
Protected methodFinalize
Allows an object to try to free resources and perform other cleanup operations before it is reclaimed by garbage collection.
(Inherited from Object.)
Public methodGetHashCode
Serves as a hash function for a particular type.
(Inherited from Object.)
Public methodGetType
Gets the type of the current instance.
(Inherited from Object.)
Protected methodMemberwiseClone
Creates a shallow copy of the current Object.
(Inherited from Object.)
Public methodToString
Returns a string that represents the current object.
(Inherited from Object.)
Remarks

The MergeField event occurs during mail merge when a simple mail merge field is encountered in the document. You can respond to this event to return text for the mail merge engine to insert into the document.

Examples
Shows how to mail merge HTML data into a document.
[C#]

// 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.
    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 object[] { htmltext });

    // Save resulting document with a new name.
    doc.Save(ArtifactsDir + "MailMerge.InsertHtml.doc");
}

private class HandleMergeFieldInsertHtml : IFieldMergingCallback
{
    /// <summary>
    /// This is called when merge field is actually merged with data in the document.
    /// </summary>
    void IFieldMergingCallback.FieldMerging(FieldMergingArgs args)
    {
        // All merge fields that expect HTML data should be marked with some prefix, e.g. 'html'.
        if (args.DocumentFieldName.StartsWith("html") && args.Field.GetFieldCode().Contains("\\b"))
        {
            FieldMergeField field = args.Field;

            // Insert the text for this merge field as HTML data, using DocumentBuilder.
            DocumentBuilder builder = new DocumentBuilder(args.Document);
            builder.MoveToMergeField(args.DocumentFieldName);
            builder.Write(field.TextBefore);
            builder.InsertHtml((string) args.FieldValue);

            // The HTML text itself should not be inserted.
            // We have already inserted it as an HTML.
            args.Text = "";
        }
    }

    void IFieldMergingCallback.ImageFieldMerging(ImageFieldMergingArgs args)
    {
        // Do nothing.
    }
}
See Also