C#: Online Guide
How To: Word SDK
Mail Merge Processing
Mail Merge Processing
  |  
Home ›› XDoc.Word ›› C# Word: Process Mail Merge

C# Word - MailMerge Processing in C#.NET


Provides C# Demo Codes for Word MeilMerge Processing to Users



Related .net document control helps:
asp.net edit pdf image: ASP.NET PDF Image Edit Control: online insert, edit PDF images in C#
asp.net edit pdf text: ASP.NET PDF Text Edit Control: online edit PDF text content using C# ASP.NET
asp.net dnn pdf editor: ASP.NET DNN PDF Editor Control: view, edit PDF files in Dotnetnuke application
asp.net tiff document viewer: ASP.NET Tiff Document Viewer: view, annotate Tiff files in HTML using C# Control
asp.net pdf file permission: ASP.NET PDF File Permission Control: apply, remove PDF file permission settings in C#
asp.net edit pdf page: ASP.NET PDF Pages Edit Control: add, remove, sort, replace PDF pages online using C#
asp.net open pdf password: ASP.NET PDF Password Edit Control: online add, remove, update PDF file open password using C#


Overview



C#.NET Word document MailMerge Interface control (XDoc.Word).//More TODO can help C# users to execute mail-merge.




C# DLLs: MailMerge Processing



Add references:


  RasterEdge.Imaging.Basic.dll


  RasterEdge.XDoc.Office.Inner.Common.dll


  RasterEdge.Imaging.Drawing.dll


  RasterEdge.Imaging.Processing.dll


  RasterEdge.XDoc.Office.Inner.Office03.dll


  RasterEdge.Imaging.Font.dll


  RasterEdge.XDoc.Word.dll


  RasterEdge.XImage.Raster.Core.dll


  RasterEdge.XImage.Raster.dll


Use corresponding namespaces;


  using RasterEdge.Imaging.Basic;


  using RasterEdge.XDoc.Word;




Execute MailMerge in OpenXML File with Data Source



In C# class programming, you can use specific APIs to process mail-merge. You may use a preset template and XML file which contains data source, and execute mail-merge by using code sample below.




//Document file path
String docFilePath = @"";
String xmlFilePath = @"";
//Open the document on local disk.
DOCXDocument document = DOCXDocument.Open();
//Create data from xml file
DataSet ds = new DataSet();
ds.ReadXml(xmlFilePath);

DataTable dt = ds.Tables[0];
int index = 0;
foreach (DataRow row in dt.Rows)
{
IDocument doc = document.GetDocument().Clone();
IMailMerge mailMgrge = doc.GetMailMerge();
if (mailMgrge != null)
{
mailMgrge.Execute(row);
}
//Save document after execute
doc.Save(@"" + index.ToString() + ".docx");
index++;
}





Execute MailMerge in Microsoft Access Database by Using Data Source(X86 Only)



You may also execute mail-merge by using data source in Microsoft Access database as follow.




String connString = @"Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" + @"" + "Customers.mdb";
String docFilePath = @"";
OleDbConnection conn = new OleDbConnection(connString);
try
{
conn.Open();
// Get data from a database.
OleDbCommand cmd = new OleDbCommand("SELECT * FROM Customers", conn);
OleDbDataAdapter da = new OleDbDataAdapter(cmd);
DataTable data = new DataTable();
da.Fill(data);
//Open the document
DOCXDocument document0 = DOCXDocument.Open(docFilePath );
int counter = 1;
// Loop though all records in the data source.
foreach (DataRow row in data.Rows)
{
// Clone the template instead of loading it from disk (for speed).
IDocument doc = document0.GetDocument().Clone();

// Execute mail merge.
IMailMerge mailMerge = doc.GetMailMerge();

if (mailMerge != null)
{
mailMerge.Execute(row);
}

// Save the document.
doc.Save(@"" + counter.ToString() + ".docx");
counter++;
}
}
catch
{
}





Execute MailMerge in Field by Using Data Source



You can also execute mail-merge in Word document by data source from string array objects as follow.




String docFilePath = @"";
//Open the document
DOCXDocument document1 = DOCXDocument.Open(docFilePath );

String[] fieldNames = new String[] {"RecipientName", "SenderName", "FaxNumber", "PhoneNumber","Subject", "Body", "Urgent", "ForReview", "PleaseComment", "Date"};

String[] fieldValues = new String[] {"Josh", "Jenny", "123456789", "8888888", "Hello","Test message 1", "one", "two", "three", "2015-01-01"};

IDocument doc2 = document1.GetDocument();

IMailMerge mailMerge0 = doc2.GetMailMerge();

if (mailMerge0 != null)
{
   mailMerge0.Execute(fieldNames, fieldValues);
}
doc2.Save(@"");