We use cookies to give you the best experience on our website. If you continue to browse, then you agree to our privacy policy and cookie policy. Image for the cookie policy date

Linking a csv data source to a Word document

Hi,

I would like to know whether it's possible to link a csv file to a Word document programmatically using DocIO.

Under MS Word, you can select a list of recipients (an OLDB data source, csv file, etc...). MS Word analyzes this data source and creates a list of available fields to merge.

What I would like to do is link my Word document to a csv file that will be used as the list of recipients.

Thank you for your help,

Gnarl


3 Replies

BP Bhuvaneswari P Syncfusion Team April 27, 2009 07:39 AM UTC

Hi Gnarl,

Thank you for your inquiry.

Currently we don't have direct support for Mail merge using CSV format. But it can be workaround by converting the CSV file to DataTable using .Net and then pass this table for mail merge.

Please refer the below code snippet to do so:

doc.MailMerge.Execute(csvToDataTable(@"..\..\test1.csv",true ));

public static DataTable csvToDataTable(string file, bool isRowOneHeader)
{

DataTable csvDataTable = new DataTable();

//no try/catch - add these in yourselfs or let exception happen
String[] csvData = File.ReadAllLines(file);

//if no data in file ‘manually’ throw an exception
if (csvData.Length == 0)
{
throw new Exception("CSV File Appears to be Empty");
}

String[] headings = csvData[0].Split(',');
int index = 0; //will be zero or one depending on isRowOneHeader

if (isRowOneHeader) //if first record lists headers
{
index = 1; //so we won’t take headings as data

//for each heading
for (int i = 0; i < headings.Length; i++)
{
//replace spaces with underscores for column names
headings[i] = headings[i].Replace(" ", "_");

//add a column for each heading
csvDataTable.Columns.Add(headings[i], typeof(string));
}
}
else //if no headers just go for col1, col2 etc.
{
for (int i = 0; i < headings.Length; i++)
{
//create arbitary column names
csvDataTable.Columns.Add("col" + (i + 1).ToString(), typeof(string));
}
}

//populate the DataTable
for (int i = index; i < csvData.Length; i++)
{
//create new rows
DataRow row = csvDataTable.NewRow();

for (int j = 0; j < headings.Length; j++)
{
//fill them
row[j] = csvData[i].Split(',')[j];
}

//add rows to over DataTable
csvDataTable.Rows.Add(row);
}

//return the CSV DataTable
return csvDataTable;

}


Please refers to the below sample which illustrate how to mail merge using CSV file:
http://www.syncfusion.com/uploads/redirect.aspx?file=Mailmerge_CSV_6f511c6a.zip&team=support

Please try this and let us know if this helps you.

Best Regards,
Bhuvana




BD Benoit de Bernardy April 28, 2009 07:43 AM UTC

Thank you Bhuvana for the quick reply.

Unfortunately, it was not an answer to the question I asked.
I’ll try to rephrase my question differently.
I got the Doc.MailMerge.Execute method to work just fine. I’m not looking for help on this part.

Under Microsoft Word, when you want to do a mail merge, you first have to select the data source. Once you have selected this data source, you get a list of all the available fields and you can add these fields into your Word document using drag and drop.
When you close a document for which you have specified a data source, when you open the document again, Microsoft Word is able to find the OLEDB data source you had specified which means that it’s stored somewhere in your Word document. That’s the information I’m trying to locate.




BP Bhuvaneswari P Syncfusion Team April 29, 2009 10:42 AM UTC

Hi Gnarl,

Thanks for more details.

I could understand your requirement. Unfortunately, there is no way to link document to Mail Merge DataSource using our Essential DocIO. We don't have any immediate plan implement this feature in our near future.

You can only perform Mail Merge programmatically as sample given in the previous update.

For more information please refer the below documentation link for DocIO mail merge features.
http://help.syncfusion.com/ug_72/docio/AdvanceMailmergeFeatures.html

Please let us know if you need any further details.

Best Regards,
Bhuvana


Loader.
Live Chat Icon For mobile
Up arrow icon