Search code examples
c#ms-wordmailmerge

c# mail merge unable to get datafields other than datafield of first record


As the title stated I am unable to retrieve any datafield besides the first record.

Below is my code.

doc.MailMerge.DataSource.FirstRecord = 1;
doc.MailMerge.DataSource.LastRecord = 5; 

    foreach (MailMergeDataField datafield in doc.MailMerge.DataSource.DataFields)
            {
                datafieldarray[datacounter] = datafield.Value.ToString();
                datacounter++;

            }

Solution

  • To parse between the datafields of Datasources, setting the FirstRecord and LastRecord properties alone would not work.

    You'd need to use ActiveRecord to change the position of the datasource, and navigate through subsequent records.

    Refer this SO answer to parse.

    MSDN link that could be helpful