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: 20.5
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
Shows how to fill MERGEFIELDs with data with a DocumentBuilder and without a mail merge.
Document doc = new Document();
DocumentBuilder builder = new DocumentBuilder(doc);

// Insert some MERGEFIELDS, which accept data from columns of the same name in a data source during a mail merge
builder.InsertField(" MERGEFIELD Chairman ");
builder.InsertField(" MERGEFIELD ChiefFinancialOfficer ");
builder.InsertField(" MERGEFIELD ChiefTechnologyOfficer ");

// They can also be filled in manually like this
builder.MoveToMergeField("Chairman");
builder.Bold = true;
builder.Writeln("John Doe");

builder.MoveToMergeField("ChiefFinancialOfficer");
builder.Italic = true;
builder.Writeln("Jane Doe");

builder.MoveToMergeField("ChiefTechnologyOfficer");
builder.Italic = true;
builder.Writeln("John Bloggs");

doc.Save(ArtifactsDir + "DocumentBuilder.FillMergeFields.docx");
Examples
Shows how to insert checkbox form fields into a document during mail merge.
public void InsertCheckBox()
{
    Document doc = new Document();
    DocumentBuilder builder = new DocumentBuilder(doc);
    builder.StartTable();
    builder.InsertCell();
    builder.InsertField(" MERGEFIELD  TableStart:StudentCourse ");
    builder.InsertCell();
    builder.InsertField(" MERGEFIELD  CourseName ");
    builder.InsertCell();
    builder.InsertField(" MERGEFIELD  TableEnd:StudentCourse ");
    builder.EndTable();

    // 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 + "MailMergeEvent.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