Mail Merge using Data from Excel File | C# .NET

Hi,

I try to use this code, but without success. What i’m trying to do is a mail merge wiht a doc witch have fields mapped and i have a excel template with the values. Manually is something like this —> open the word —> mailings —>select recipients—>Use an existing list…—> and select excel file.

I used the code but i get an messagem indicanting that the file word is trying to access to the excel file.

I thougt that the code transforming automatically in new doc already mapping fields.

Can you help?.

Thanks.

Hugo Castro

@hugocastro,

Standard ways of executing mail merge using the data coming from various sources are mentioned in the following section of documentation:

Secondly, I think you can use “Aspose.Cells for .NET” API to first export Excel data to DataTable without any formatting and then use “Aspose.Words for .NET” API’s MailMerge.Execute(DataTable) Method overload.

Hi,

I’m getting “Method not found:'Aspose.words.mailmerging.mailmerge Aspose.words.Document.get_MailMerge()” when the code is executed.

Any ideas what could be?

thanks.

Best regards.

Hugo Castro

@hugocastro,

Please tell what Aspose.Words API are you getting this problem with (“Aspose.Words for .NET” or “Aspose.Words for Java” or “Aspose.Words for C++”)? Also, please compress your simplified .NET/Java/C++ application into ZIP format and attach the source code here for testing. Please do not include DLL or JAR files in it to reduce the size of ZIP. We will then investigate the issue on our end and provide you more information.

Hi,

I’m using Aspose.Words for .NET.
I can’t give to you all application due company restrictions, but i can’t put here the code (right now is very simple):

		    Aspose.Words.License license = new Aspose.Words.License();
			license.SetLicense("Aspose.Total.NET.lic");
                        Document doc = new Document((string)fileName);
  				   doc.MailMerge.Execute(
                                    new string[] { "NumeroDocumento" },
                                    new object[] { "I-000004-2021" }
                                );
                       doc.Save((string)resultFileName);

I Have reference for aspose.words and a license as you can see on attachments.

There’s one thing that i’m using Outystems Platform and i have to integrate this application, by integration studio. But have done the same for other application (convert word do pdf) and works fine.

lic.PNG (10.9 KB)
asposeWords.PNG (13.2 KB)

What i’m doing wrong?

Many thanks.

Regards.

Hugo Castro

Hi,

I managed to overcome this problem, i had to delete the file aspose.word.xml that was generated to the apllication generate a new one (updated).

I need help to other thing, i need to do mail merge to a docoment has already MERGEFIELDs and i have 2 ways:

  • I have an excel with the colums and the values for mergefields;
  • I have a record list with colums and the values for mergefields (example:

first row
column: date
value: 2020-10-01

second row
column: subject
value: test
)

I tried very times, but without success.

Can you give me simple example how to do in these two cases

thanks.

Regards

Hugo Castro

@hugocastro,

Once you have excel file converted to DataTable, you can then perform mail merge by using Aspose.Words like this:

Document doc = new Document("C:\\Temp\\Date.docx");
doc.MailMerge.Execute(GetDataTable());
doc.Save("C:\\Temp\\21.9.docx");

private static DataTable GetDataTable()
{
    DataTable dataTable = new DataTable("table");

    dataTable.Columns.Add(new DataColumn("Date"));
    dataTable.Columns.Add(new DataColumn("Subject"));

    DataRow dataRow = dataTable.NewRow();
    dataRow[0] = DateTime.Now;
    dataRow[1] = "test";

    dataTable.Rows.Add(dataRow);

    return dataTable;
}

Hi,

Thank you for the explanation. Now it’s working perfectly.

For others who have the same problem, I leave the code (for mail merge from excel file converted to datatable) I used:

                    DataTable dt = new DataTable();

                   //datafilename is the paht for the xls file
                    FileStream fs = new FileStream(dataFileName, FileMode.Open);

                    Workbook wb = new Workbook(fs);
                    
                     //access excel sheet
                    Worksheet worksheet = wb.Worksheets[0];

                    //access excel sheet
                    Cell cell = worksheet.Cells.LastCell;


                    dt = worksheet.Cells.ExportDataTable(0, 0, 2, cell.Column + 1, true);

                    fs.Close();

                    doc.MailMerge.Execute(dt);

                    doc.Save(resultFileName);

Best regards.

Hugo Castro

@hugocastro,

You are right; the code loads XLS file with Aspose.Cells for .NET API, then populates DataTable by using the ExportDataTable method and finally performs mail merge on Word template by using Aspose.Words for .NET API. Thanks for sharing your solution which may also help other developers who are looking to solve similar problem.