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
Syntax
public bool MoveToMergeField(
	string fieldName
)

Parameters

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.
Remarks

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

Examples
Fills document merge fields with some data.
[C#]

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

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

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

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

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

// 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();
    doc.MailMerge.ExecuteWithRegions(dataTable);

    // 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.MoveToMergeField(args.FieldName);
            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);

            builder.Write(fieldValue);
            mCheckBoxCount++;
        }
    }

    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");
    dataTable.Columns.Add("CourseName");
    for (int i = 0; i < 10; i++)
    {
        DataRow datarow = dataTable.NewRow();
        dataTable.Rows.Add(datarow);
        datarow[0] = "Course " + i;
    }

    return dataTable;
}
See Also