MailMerge - How to set an Excel file as data source?

Hi,

I’m trying to find the proper way of setting an excel file as a data source for mail merge.

Should I manually set the MailMergeSettings.ConnectionString, DataType, Query, etc?

Or is there a simpler way of doing that, given the path to the Excel file and the name of a sheet?

Thanks,
Joao

Hi Joao,

Thanks for your inquiry. You can easily use Excel document as data source. Since you can easily select data from excel file using ADO.NET. It would be very easy to use these data for mail merge.
http://support.microsoft.com/kb/316934

You can also connect an Excel spreadsheet as an external data source to your input Word template document via the dynamic data exchange (DDE) system by using the code something like below:

Document doc = new Document(@"C:\Temp\Doc1.docx");
string excelDS = @"c:\Temp\Book1.xlsx";
MailMergeSettings mms = doc.MailMergeSettings;
mms.DataSource = excelDS;
mms.MainDocumentType = MailMergeMainDocumentType.FormLetters;
mms.DataType = MailMergeDataType.Spreadsheet;
mms.LinkToQuery = true;
mms.ViewMergedData = true;
mms.Query = "SELECT * FROM " + mms.DataSource;
doc.Save(@"c:\Temp\out.docx");

I hope, this helps.

Best regards,

A post was split to a new topic: Mail Merge using Data from Excel File | C# .NET