DocumentBuilderMoveToMergeField Method (String)
Moves the cursor to a position just beyond the specified merge field and removes the merge field.

Namespace: Aspose.Words
Assembly: Aspose.Words (in Aspose.Words.dll) Version: 19.10
public bool MoveToMergeField(
	string fieldName


Type: SystemString
The case-insensitive name of the mail merge field.

Return Value

Type: Boolean
True if the merge field was found and the cursor was moved; false otherwise.

Note that this method deletes the merge field from the document after moving the cursor.

Fills document merge fields with some data.

Document doc = new Document(MyDir + "DocumentBuilder.FillingDocument.doc");
DocumentBuilder builder = new DocumentBuilder(doc);

builder.Bold = true;
builder.Writeln("Roman Korchagin");

builder.Italic = true;
builder.Writeln("Dmitry Vorobyev");

builder.Italic = true;
builder.Writeln("Vladimir Averkin");

doc.Save(ArtifactsDir + "DocumentBuilder.FillingDocument.doc");
Shows how to insert checkbox form fields into a document during mail merge.

// File 'MailMerge.InsertCheckBox.doc' is a template
    // containing the table with the following fields in it:
    // <<TableStart:StudentCourse>> <<CourseName>> <<TableEnd:StudentCourse>>.
    Document doc = new Document(MyDir + "MailMerge.InsertCheckBox.doc");

    // Add a handler for the MergeField event.
    doc.MailMerge.FieldMergingCallback = new HandleMergeFieldInsertCheckBox();

    // Execute mail merge with regions.
    DataTable dataTable = GetStudentCourseDataTable();

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

private class HandleMergeFieldInsertCheckBox : IFieldMergingCallback
    /// <summary>
    /// This is called for each merge field in the document
    /// when Document.MailMerge.ExecuteWithRegions is called.
    /// </summary>
    void IFieldMergingCallback.FieldMerging(FieldMergingArgs args)
        if (args.DocumentFieldName.Equals("CourseName"))
            // The name of the table that we are merging can be found here
            Assert.AreEqual("StudentCourse", args.TableName);

            // Insert the checkbox for this merge field, using DocumentBuilder.
            DocumentBuilder builder = new DocumentBuilder(args.Document);
            builder.InsertCheckBox(args.DocumentFieldName + mCheckBoxCount, false, 0);

            // Get the actual value of the field
            string fieldValue = args.FieldValue.ToString();

            // In this case, for every record index 'n', the corresponding field value is "Course n"
            Assert.AreEqual(Char.GetNumericValue(fieldValue[7]), args.RecordIndex);


    void IFieldMergingCallback.ImageFieldMerging(ImageFieldMergingArgs args)
        // Do nothing.

    /// <summary>
    /// Counter for CheckBox name generation
    /// </summary>
    private int mCheckBoxCount;

/// <summary>
/// Create DataTable and fill it with data.
/// In real life this DataTable should be filled from a database.
/// </summary>
private static DataTable GetStudentCourseDataTable()
    DataTable dataTable = new DataTable("StudentCourse");
    for (int i = 0; i < 10; i++)
        DataRow datarow = dataTable.NewRow();
        datarow[0] = "Course " + i;

    return dataTable;
See Also