Aspose Words Execute Mail Merge for Document with Existing Text File Data Source

Hi,

I have a doc(x) which is ALREADY created as a mail merge document. The document’s Datasource is an existing tab delimited text file stored on the local file system.

The tab delimited text file contains multiple records. ie: 10 records in the text file.

I want to perform the mail merge of the document and send the output to either a printer or a new document.

I have discovered the MailMergeSettings property of the wordDocument.

            Dim mms As Settings.MailMergeSettings = wordDocument.MailMergeSettings
            mms.MainDocumentType = Settings.MailMergeMainDocumentType.FormLetters
            mms.DataType = Settings.MailMergeDataType.TextFile
            mms.LinkToQuery = False
            mms.ViewMergedData = True
            mms.Destination = Settings.MailMergeDestination.Printer

The problem is how do I actually execute the mail merge? The wordDoc.MailMerge.Execute() method requires does not have an overload which accepts ZERO/No Parameters.

I just want the document to mailmerge using the CURRENT datasource as it is defined in the document (and seemingly exposed via the MailMergeSetttings.

for each
Regards
Aaron

@aarondglover,

I think, the easy/simple way is to build a C# DataTable object by reading/parsing the Text file containing the ‘data’, execute mail merge with regions on template document by using Aspose.Words and finally convert to DOCX/PDF. Please see the sample documents (MailMerge-ExecuteWithRegions-Sample-Documents.zip (23.7 KB)) and the following draft code:

Document doc = new Document("E:\\temp\\TableStart-MailMerge-Template.docx");
Document docData = new Document("E:\\temp\\datasource.txt");

DataTable dataTable = new DataTable("Data");

Paragraph columnNames = docData.FirstSection.Body.FirstParagraph;
string[] columnNamesSplits = columnNames.ToString(SaveFormat.Text).Split(new char[] { ControlChar.TabChar });

foreach (string name in columnNamesSplits)
    dataTable.Columns.Add(new DataColumn(name.Trim()));

for (int i = 1; i < docData.FirstSection.Body.Paragraphs.Count; i++)
{
    Paragraph row = docData.FirstSection.Body.Paragraphs[i];
    string[] rowSplits = row.ToString(SaveFormat.Text).Split(new char[] { ControlChar.TabChar });

    int count = 0;
    DataRow dataRow;
    dataRow = dataTable.NewRow();
    foreach (string name in rowSplits)
    {
        dataRow[count++] = name.Trim();
    }

    dataTable.Rows.Add(dataRow);
}

doc.MailMerge.ExecuteWithRegions(dataTable);

doc.Save("E:\\Temp\\19.11.PDF");

Hope, this helps in achieving what you are looking for.