Search Results for

    Show / Hide Table of Contents

    Class MailMerge

    Provides mail merge functionality for a WordDocument.

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

    Populating a Word template with data, names on a batch of letters, line items on an invoice, rows in a report, runs through MailMerge. It detects the merge fields already in a document, whether they were authored in Microsoft Word or another compatible library, and fills them from the data a developer supplies. It is the type behind a "C# mail merge" search, and it sits alongside MergeField, the read-only record describing one discovered field, and MailMergeOptions, the settings that govern the run.

    A document hands back its merge facade through the get-only WordDocument.MailMerge property, so there is no separate object to construct. From there the members fall into four groups. **Discovery**: GetFieldNames lists every merge field name, GetFields returns the richer MergeField records, and GetRegionNames reports the repeating-region names. **Flat merge**: the Execute overloads fill the fields once, accepting an IDictionary<string, string>, a parallel pair of IEnumerable<string> names and values, a DataRow, or a DataTable (whose first row supplies the values). **Region merge**: the ExecuteWithRegions overloads repeat a block of the template per data row, accepting a DataSet, a DataTable, or a region name paired with a DataTable, driven by fields marked TableStart:Region and TableEnd:Region. **Configuration**: the Options property exposes a MailMergeOptions for null handling and unused-field removal.

    The usual sequence is to read the field names, build matching data, then call one Execute overload for a single record or ExecuteWithRegions for a repeating section. Configure Options first if the defaults need adjusting, then save the document as usual.

    WordDocument document = new WordDocument("template.docx");
    document.MailMerge.Execute(new Dictionary<string, string>
    {
        ["FirstName"] = "Ada",
        ["Company"] = "Iron Software"
    });
    document.Save("letter.docx");

    The replace words how-to covers replacing field text, the create word from text example builds a document to merge into, and the document element tutorial explains the document model around it.

    Properties

    Options

    Gets the options that control how this MailMerge instance behaves.

    Declaration
    public MailMergeOptions Options { get; }
    Property Value
    Type Description
    MailMergeOptions

    Methods

    Execute(IDictionary<String, String>)

    Replaces every merge field whose name appears in data with the matching value. Fields that have no entry are removed (when RemoveUnusedFields is true) or left in place.

    Declaration
    public void Execute(IDictionary<string, string> data)
    Parameters
    Type Name Description
    System.Collections.Generic.IDictionary<System.String, System.String> data

    A dictionary keyed by merge field name.

    Execute(IEnumerable<String>, IEnumerable<String>)

    Replaces fields named in fieldNames with the corresponding entries in values.

    Declaration
    public void Execute(IEnumerable<string> fieldNames, IEnumerable<string> values)
    Parameters
    Type Name Description
    System.Collections.Generic.IEnumerable<System.String> fieldNames
    System.Collections.Generic.IEnumerable<System.String> values
    Exceptions
    Type Condition
    System.ArgumentException

    Thrown when fieldNames and values are different lengths.

    Execute(DataRow)

    Replaces fields using the column names of dataRow as field names.

    Declaration
    public void Execute(DataRow dataRow)
    Parameters
    Type Name Description
    System.Data.DataRow dataRow

    Execute(DataTable)

    Replaces fields using the column names of dataTable as field names and the first row's values. Equivalent to calling Execute(DataRow) with dataTable.Rows[0].

    Declaration
    public void Execute(DataTable dataTable)
    Parameters
    Type Name Description
    System.Data.DataTable dataTable

    ExecuteWithRegions(DataSet)

    Expands every TableStart/TableEnd region in the document by repeating its content once for each row of the matching table in dataSet.

    Declaration
    public void ExecuteWithRegions(DataSet dataSet)
    Parameters
    Type Name Description
    System.Data.DataSet dataSet

    A System.Data.DataSet containing tables that correspond to region names defined by TableStart:RegionName and TableEnd:RegionName fields.

    Remarks

    Tables are matched to regions by name.

    Non-region merge fields are not populated by this method. To populate standard merge fields, call Execute(IDictionary<String, String>) after region expansion.

    ExecuteWithRegions(DataTable)

    Expands a single region using dataTable's System.Data.DataTable.TableName as the region name.

    Declaration
    public void ExecuteWithRegions(DataTable dataTable)
    Parameters
    Type Name Description
    System.Data.DataTable dataTable

    ExecuteWithRegions(String, DataTable)

    Expands the named region with rows from dataTable.

    Declaration
    public void ExecuteWithRegions(string regionName, DataTable dataTable)
    Parameters
    Type Name Description
    System.String regionName
    System.Data.DataTable dataTable

    GetFieldNames()

    Gets the names of every value-style merge field declared in the document (in document order, deduplicated).

    Declaration
    public IReadOnlyList<string> GetFieldNames()
    Returns
    Type Description
    System.Collections.Generic.IReadOnlyList<System.String>

    GetFields()

    Gets every merge field discovered in the document, including TableStart/TableEnd markers.

    Declaration
    public IReadOnlyList<MergeField> GetFields()
    Returns
    Type Description
    System.Collections.Generic.IReadOnlyList<MergeField>

    GetRegionNames()

    Gets the names of every TableStart region declared in the document.

    Declaration
    public IReadOnlyList<string> GetRegionNames()
    Returns
    Type Description
    System.Collections.Generic.IReadOnlyList<System.String>
    ☀
    ☾
    Downloads
    • Download with NuGet
    • Start for Free
    In This Article
    Back to top
    Install with NuGet
    %s requires a valid license key for further deployment. Get Trial Below.
    ironsuite
    Blue key in circleGet Started for Free
    30 days full access • No credit card required
    Test in a live environment

    Test in production without watermarks.
    Works wherever you need it to.

    Fully-functional product

    Get 30 days of fully functional product.
    Have it up and running in minutes.

    24/5 technical support

    Full access to our support engineering team during your product trial

    Grey key in circleGet Started for Free
    The trial form was submitted successfully.
    Grey check in circleTrial key delivered
    Your key has been issued and sent to your email.
    Calendar in circleBook Free Live Demo
    No cards • no pitches • no commitments Pick a time for a quick, personalized demo
    Get the most out of your 30-day trial with:

    Live product and key features walk-through

    Project-specific recommendations

    All your questions answered

    Grey key in circleBook Free Live Demo
    Your booking has been completed Check your e-mail for confirmation

    Want to deploy Iron Suite
    to a live project for FREE?

    What’s included?

    • Test in production without watermarks
    • 30 days fully functional product
    • 24/5 technical support during trial
    ironpdf Logo
    ironword Logo
    ironxl Logo
    ironppt Logo
    ironocr Logo
    ironbarcode Logo
    ironqr Logo
    ironprint Logo
    ironzip Logo
    ironwebscraper Logo
    Iron Software sales team
    Online 24/5
    Have Enterprise licensing requirements?
    Request Iron Suite Enterprise trial
    Key in blue circle

    Get your free 30-day Trial Key instantly

    Your trial license will be sent to your email address

    No limitations. 100% unlocked. No credit card.

    bullet_checkedNo credit card or account creation requiredNo limitations. 100% unlocked. No credit card.
    Or
    Key in blue circle

    Complete Your Trial Key Signup

    Tell us a little more about yourself.

    Key in blue circleNo credit card or account creation requiredNo limitations. 100% unlocked. No credit card.
    Key in blue circle

    Your license key has been delivered to your inbox

    Can't find it? Check your spam folder.

    What’s next?
    Install with NuGet
    Add IronWord to your project
    Quickstart Guide
    Helpful resources to get started

    Need help getting started?

    Book a Free Live Demo

    See IronWord in action and get your licensing questions answered, or

    Key in blue circle

    Your demo request is in.

    You'll receive an invite via email shortly.

    What’s next?
    Explore Documentation
    Review Licensing Options
    badge_greencheck_in_yellowcircle
    Thank you
    Your trial key should be in the email.
    If it is not, please contact support@ironsoftware.com
    • Logo Aetna
    • Logo NASA
    • Logo GE
    • Logo Porsche
    • Logo USDA
    • Logo Qatar
    Join Millions of Engineers who’ve tried IronWord
    Your trial key should be in the email.
    If it is not, please contact support@ironsoftware.com