soapygus
asked on
Basic Word Mail Merge functionality with C#.NET - given Data File and Merge Doc
Hello Experts,
I'm working with MS Office 2003 and visual studio 2005
I have experience with C# .NET and web applications, but I have not yet worked with Microsoft Office .. . until now. I'm trying to understand how to automate a mail merge and am looking for a little guidance.
I have a simple Windows Application with a button. Given a data source file and and merge document, I would like to perform a merge with the OnClick event.
My merge document is a *.doc.
My data source is a *.xls.
I am hoping there is a method that takes 2 arguments: one for data source file, one for merge document. Does this exist? I'm having trouble understanding the documentation that I have found. It seems all the examples create a data source file from scratch and I'm not understanding the call that actually merges the data.
Can someone point me in the right direction?
Thanks.
I'm working with MS Office 2003 and visual studio 2005
I have experience with C# .NET and web applications, but I have not yet worked with Microsoft Office .. . until now. I'm trying to understand how to automate a mail merge and am looking for a little guidance.
I have a simple Windows Application with a button. Given a data source file and and merge document, I would like to perform a merge with the OnClick event.
My merge document is a *.doc.
My data source is a *.xls.
I am hoping there is a method that takes 2 arguments: one for data source file, one for merge document. Does this exist? I'm having trouble understanding the documentation that I have found. It seems all the examples create a data source file from scratch and I'm not understanding the call that actually merges the data.
Can someone point me in the right direction?
Thanks.
ASKER
Thanks GrahamSkan,
I've got it working with one small problem. My OnClick event is referencing the correct files (template.doc, and datafile.xls); however, when it executes the merge I get a prompt asking me to "Select Table." My xls only has one worksheet so the worksheet is selected and a checkbox is checked that indicate that the first row is my header row.
I click "Ok" and the merge executes perfectly.
I just need to figure out how to specify the header row is my first record in the XLS.
Here's the code I've been working with.
// Create an instance of Word and make it visible.
wrdApp = new Word.Application();
wrdApp.Visible = true;
Word.Document myMergeDocument;
string strFileName = "c:\\template.doc";
string strDataFile = "c:\\datafile.xls";
object filename = strFileName;
object objTrue = true;
object objFalse = false;
object objMiss = Type.Missing;
//Open merge document
myMergeDocument = wrdApp.Documents.Open(ref filename, ref objMiss, ref objMiss, ref objMiss, ref objMiss, ref objMiss, ref objMiss, ref objMiss, ref objMiss, ref objMiss, ref objMiss, ref objMiss, ref objMiss, ref objMiss, ref objMiss, ref objMiss);
myMergeDocument.Select();
//Open the data source
object source = strDataFile;
object format = Word.WdOpenFormat.wdOpenFo rmatAuto;
myMergeDocument.MailMerge. OpenDataSo urce(strDa taFile, ref format, ref objFalse, ref objMiss, ref objTrue, ref objFalse, ref objMiss, ref objMiss, ref objMiss, ref objMiss, ref objMiss, ref objMiss, ref objMiss, ref objMiss, ref objMiss, ref objMiss);
//Perform Merge
myMergeDocument.MailMerge. Destinatio n = Word.WdMailMergeDestinatio n.wdSendTo NewDocumen t;
myMergeDocument.MailMerge. SuppressBl ankLines = true;
myMergeDocument.MailMerge. DataSource .FirstReco rd = (int)Word.WdMailMergeDefau ltRecord.w dDefaultFi rstRecord;
myMergeDocument.MailMerge. DataSource .LastRecor d = (int)Word.WdMailMergeDefau ltRecord.w dDefaultLa stRecord;
myMergeDocument.MailMerge. Execute(re f objFalse);
I've got it working with one small problem. My OnClick event is referencing the correct files (template.doc, and datafile.xls); however, when it executes the merge I get a prompt asking me to "Select Table." My xls only has one worksheet so the worksheet is selected and a checkbox is checked that indicate that the first row is my header row.
I click "Ok" and the merge executes perfectly.
I just need to figure out how to specify the header row is my first record in the XLS.
Here's the code I've been working with.
// Create an instance of Word and make it visible.
wrdApp = new Word.Application();
wrdApp.Visible = true;
Word.Document myMergeDocument;
string strFileName = "c:\\template.doc";
string strDataFile = "c:\\datafile.xls";
object filename = strFileName;
object objTrue = true;
object objFalse = false;
object objMiss = Type.Missing;
//Open merge document
myMergeDocument = wrdApp.Documents.Open(ref filename, ref objMiss, ref objMiss, ref objMiss, ref objMiss, ref objMiss, ref objMiss, ref objMiss, ref objMiss, ref objMiss, ref objMiss, ref objMiss, ref objMiss, ref objMiss, ref objMiss, ref objMiss);
myMergeDocument.Select();
//Open the data source
object source = strDataFile;
object format = Word.WdOpenFormat.wdOpenFo
myMergeDocument.MailMerge.
//Perform Merge
myMergeDocument.MailMerge.
myMergeDocument.MailMerge.
myMergeDocument.MailMerge.
myMergeDocument.MailMerge.
myMergeDocument.MailMerge.
ASKER CERTIFIED SOLUTION
membership
Create a free account to see this answer
Signing up is free and takes 30 seconds. No credit card required.
ASKER
Thank you!
Programatically you can use the OpenDataSource method to link a particular data source to the document. Hopefully it will have the same field names as the datasource that document was designed for.
A document object has a Mailmerge Property which holds the Datasource object and has an Execute method to actually run the merge.
I don't use C#, but if you can understand VB, the Help in Word's VBA editor should be able to help you more.