FieldMergeField

FieldMergeField class

Implements the MERGEFIELD field.

To learn more, visit the Working with Fields documentation article.

public class FieldMergeField : Field

Properties

NameDescription
DisplayResult { get; }Gets the text that represents the displayed field result.
End { get; }Gets the node that represents the field end.
FieldName { get; set; }Gets or sets the name of a data field.
FieldNameNoPrefix { get; }Returns just the name of the data field. Any prefix is stripped to the prefix property.
Format { get; }Gets a FieldFormat object that provides typed access to field’s formatting.
IsDirty { get; set; }Gets or sets whether the current result of the field is no longer correct (stale) due to other modifications made to the document.
IsLocked { get; set; }Gets or sets whether the field is locked (should not recalculate its result).
IsMapped { get; set; }Gets or sets whether this field is a mapped field.
IsVerticalFormatting { get; set; }Gets or sets whether to enable character conversion for vertical formatting.
LocaleId { get; set; }Gets or sets the LCID of the field.
Result { get; set; }Gets or sets text that is between the field separator and field end.
Separator { get; }Gets the node that represents the field separator. Can be null.
Start { get; }Gets the node that represents the start of the field.
TextAfter { get; set; }Gets or sets the text to be inserted after the field if the field is not blank.
TextBefore { get; set; }Gets or sets the text to be inserted before the field if the field is not blank.
override Type { get; }Gets the Microsoft Word field type.

Methods

NameDescription
GetFieldCode()Returns text between field start and field separator (or field end if there is no separator). Both field code and field result of child fields are included.
GetFieldCode(bool)Returns text between field start and field separator (or field end if there is no separator).
Remove()Removes the field from the document. Returns a node right after the field. If the field’s end is the last child of its parent node, returns its parent paragraph. If the field is already removed, returns null.
Unlink()Performs the field unlink.
Update()Performs the field update. Throws if the field is being updated already.
Update(bool)Performs a field update. Throws if the field is being updated already.

Remarks

Retrieves the name of a data field within the merge characters in a mail merge main document. When the main document is merged with the selected data source, information from the specified data field is inserted in place of the merge field.

Examples

Shows how to use MERGEFIELD fields to perform a mail merge.

Document doc = new Document();
DocumentBuilder builder = new DocumentBuilder(doc);

// Create a data table to be used as a mail merge data source.
DataTable table = new DataTable("Employees");
table.Columns.Add("Courtesy Title");
table.Columns.Add("First Name");
table.Columns.Add("Last Name");
table.Rows.Add("Mr.", "John", "Doe");
table.Rows.Add("Mrs.", "Jane", "Cardholder");

// Insert a MERGEFIELD with a FieldName property set to the name of a column in the data source.
FieldMergeField fieldMergeField = (FieldMergeField)builder.InsertField(FieldType.FieldMergeField, true);
fieldMergeField.FieldName = "Courtesy Title";
fieldMergeField.IsMapped = true;
fieldMergeField.IsVerticalFormatting = false;

// We can apply text before and after the value that this field accepts when the merge takes place.
fieldMergeField.TextBefore = "Dear ";
fieldMergeField.TextAfter = " ";

Assert.AreEqual(" MERGEFIELD  \"Courtesy Title\" \\m \\b \"Dear \" \\f \" \"", fieldMergeField.GetFieldCode());

// Insert another MERGEFIELD for a different column in the data source.
fieldMergeField = (FieldMergeField)builder.InsertField(FieldType.FieldMergeField, true);
fieldMergeField.FieldName = "Last Name";
fieldMergeField.TextAfter = ":";

doc.UpdateFields();
doc.MailMerge.Execute(table);

Assert.AreEqual("Dear Mr. Doe:\u000cDear Mrs. Cardholder:", doc.GetText().Trim());

See Also