Search Results for

    Show / Hide Table of Contents

    Class MergeField

    Represents a discovered mail merge field within a Word document.

    Inheritance
    System.Object
    MergeField
    Namespace: IronWord.Models.MailMerge
    Assembly: IronWord.dll
    Syntax
    public class MergeField : Object

    A MergeField is the record IronWord hands back for each merge field it discovers in a Word document, so a developer can inspect a template's fields before supplying data. Each one describes a single placeholder: the field name to match against a data source, the raw field instruction stored in the document, and what role the field plays.

    Get the fields from a MailMerge by calling GetFields, which returns an IReadOnlyList<MergeField>. Name is the field name parsed from the instruction, the key to match against a column or dictionary entry. Instruction is the full field instruction text as stored (for example a MERGEFIELD directive). Kind is a MergeFieldKind that tells a regular value field apart from a region marker or a NEXT-record field, and RegionName carries the region name when the field begins or ends a repeating region, otherwise an empty string. ToString gives a readable summary of the field. Reading these is how you confirm a template's fields line up with the data before running the merge. The replace words how-to covers replacing field text, and the document element tutorial explains the document model these fields sit in.

    foreach (MergeField field in document.MailMerge.GetFields())
        Console.WriteLine(field.Name);

    Constructors

    MergeField(String, String, MergeFieldKind)

    Initializes a new instance of the MergeField class.

    Declaration
    public MergeField(string name, string instruction, MergeFieldKind kind)
    Parameters
    Type Name Description
    System.String name

    The merge field name parsed from the field instruction.

    System.String instruction

    The full field instruction text (e.g. " MERGEFIELD FirstName * MERGEFORMAT ").

    MergeFieldKind kind

    The kind of merge field.

    Properties

    Instruction

    Gets the full field instruction text as stored in the document (e.g. MERGEFIELD FirstName * MERGEFORMAT ).

    Declaration
    public string Instruction { get; }
    Property Value
    Type Description
    System.String

    Kind

    Gets the kind of merge field (a regular value field, a region marker, or a Next-record marker).

    Declaration
    public MergeFieldKind Kind { get; }
    Property Value
    Type Description
    MergeFieldKind

    Name

    Gets the merge field name (the identifier following MERGEFIELD in the instruction).

    Declaration
    public string Name { get; }
    Property Value
    Type Description
    System.String

    RegionName

    Gets the region name when Kind is TableStart or TableEnd; otherwise an empty string.

    Declaration
    public string RegionName { get; }
    Property Value
    Type Description
    System.String

    Methods

    ToString()

    Declaration
    public override string ToString()
    Returns
    Type Description
    System.String
    ☀
    ☾
    Downloads
    • Download with NuGet
    • Start for Free
    In This Article
    Back to top
    Install with NuGet